基本上我想做的是:
因此,第三步是棘手的部分。使用AJAX,我调用下面看到的deleteChannel(url),它在代码隐藏中调用适当的方法。下面是棘手的部分:
if (channel != null && channel.Location != null)
{
// How do I do this? (Notice that this is code-behind)
ShowDialogThatAsksUserToConfirm()
}代码:ShowDialogThatAsksUserToConfirm()需要调用客户端,说“Location列不为null",然后等待用户说”删除“或”取消“。
我有这样的代码,它将在代码后面调用方法:
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');
}
});
}和showDialog(.)看起来是这样的:
function showDialog(divID) {
$("#" + divID).dialog({
show: "clip",
hide: "clip",
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
}代码隐藏如下所示:
[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, but it should ask user to confirm
{
context.Channels.Remove(channel);
context.SaveChanges();
List<ChannelModel> updatedChannelList = new List<ChannelModel>();
context.Channels.AddRange(context.Channels);
return View("ChannelDetails", updatedChannelList);
}
}
}以下是视图:
<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 a location. Are you sure you want to delete?</p>
</div>
<div id="successDiv" title="Info" style="display: none;">
<p style="color: green;">Deleted</p>
</div>
这只是我的方法,不是最后的,如果有更好的解决办法,请告诉我。
发布于 2015-04-22 18:31:49
您不能从代码背后调用js,但是ajax方法可以返回询问用户的指示:
if (channel != null && channel.Location != null)
{
return 'cannot-delete';
}ajax方法将看到它的成功函数,并提示用户:
$.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');
}
});方法ShowDialogThatAsksUserToConfirm应该要求用户在javascript中确认,如果允许的话,提交一个强制删除记录。
https://stackoverflow.com/questions/29805916
复制相似问题