基本上我想做的是:
所以第3步是棘手的部分。使用AJAX,我调用下面看到的deleteChannel(url)
,它在代码隐藏中调用正确的方法。现在这里是棘手的部分:
if (channel != null && channel.Location != null)
{
// How do I do this? (Notice that this is code-behind)
ShowDialogThatAsksUserToConfirm()
}
代码:ShowDialogThatAsksUserToConfirm()
需要回拨给客户端说"位置列不为空",并等待用户反正删除或取消。
我有这个代码,它将在后面的代码中调用方法:
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>
这只是我的方法,并不是最终的,如果有更好的解决方案,请告诉我
答案 0 :(得分:2)
您无法从代码隐藏中调用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中询问用户的确认,如果允许,则提交强制删除记录。