javascript - Call js function from code-behind (Not as startupScript) -
basically want is:
- click on button delete record.
- delete record if specific column (location) null (this done , works fine)
- delete record, if specific column not null, ask user confirm.
so step 3 tricky part. ajax, call deletechannel(url)
see below, calls proper method in code-behind. here tricky part:
if (channel != null && channel.location != null) { // how do this? (notice code-behind) showdialogthatasksusertoconfirm() }
the code: showdialogthatasksusertoconfirm()
needs call client saying "the location column not null", , wait user delete anyway, or cancel.
i have code call method in code behind:
function deletechannel(url) { $.ajax({ url: url, type: "post", cache: false, contenttype: 'application/html; charset=utf-8', data: $("form").serialize(), datatype: 'html', success: function (msg) { showdialog('successdiv'); }, error: function (msg) { alert("error"); showdialog('errordiv'); } }); }
and showdialog(...) looks this:
function showdialog(divid) { $("#" + divid).dialog({ show: "clip", hide: "clip", buttons: { ok: function () { $(this).dialog("close"); } } }); }
the code-behind looks this:
[httppost] public actionresult deletechannel(int id) { var statuscode = new httpstatuscode(); using (var context = new maanegriscontext()) { var channel = context.channels.firstordefault(x => x.id == id); if (channel != null && channel.location != null) { if (confirmation()) //the confirmation() method not implemented yet, should ask user confirm { context.channels.remove(channel); context.savechanges(); list<channelmodel> updatedchannellist = new list<channelmodel>(); context.channels.addrange(context.channels); return view("channeldetails", updatedchannellist); } } }
here view:
<table style="border: ridge 1px"> <tr> <th>name</th> ... </tr> @{ foreach (var item in model) { <tr> <td>@html.displayfor(m => item.channelname)</td> <td>@html.displayfor(m => item.description)</td> <td>@html.displayfor(m => item.unit)</td> <td>@html.displayfor(m => item.location.stablename)</td> <td>@html.displayfor(m => item.creationdate)</td> <td>@html.displayfor(m => item.lastedited)</td> <td>@html.displayfor(m => item.extranote)</td> <td><a href="@url.action("copychannel", "channel", new { id = item.id })"><span class="glyphicon glyphicon-copy"></span></a></td> <td><a href="@url.action("editchannel", "channel", new { id = item.id })"><span class="glyphicon glyphicon-edit"></span></a></td> <td><a href="#" onclick="deletechannel('@url.action("deletechannel", "channel", new { id = item.id })')"> <span class="glyphicon glyphicon-remove"></span></a> </td> </tr> } } </table> <br/><br/> <div style="display: none;"> @* @{ html.renderpartial("_createchannel"); } *@ </div> <h5>@html.actionlink("opret en ny kanal", "registerchannel", "channel")</h5> <div id="cautiondiv" title="bekræft sletning" style="display: none;"> <p style="color: red;">the channel has location. sure want delete?</p> </div> <div id="successdiv" title="info" style="display: none;"> <p style="color: green;">deleted</p> </div>
this approach, , not final, if there better solutions, please let me know
you can't call js codebehind, ajax method can return indication asking user:
if (channel != null && channel.location != null) { return 'cannot-delete'; }
and ajax method see on it's success function, , prompt user:
$.ajax({ url: url, type: "post", cache: false, contenttype: 'application/html; charset=utf-8', data: $("form").serialize(), datatype: 'html', success: function (msg) { if (msg == 'cannot-delete') { showdialogthatasksusertoconfirm(); } else showdialog('successdiv'); }, error: function (msg) { alert("error"); showdialog('errordiv'); } });
the method showdialogthatasksusertoconfirm
should ask users's confirmation in javascript, , if allowed, submit forced delete of record.
Comments
Post a Comment