首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从代码背后调用js函数(不像startupScript那样)

从代码背后调用js函数(不像startupScript那样)
EN

Stack Overflow用户
提问于 2015-04-22 18:23:53
回答 1查看 72关注 0票数 1

基本上我想做的是:

  1. 单击按钮删除记录。
  2. 如果特定列(位置)为null,则删除记录(此操作完成并工作良好)
  3. 删除记录,但如果特定列不为空,则请用户确认。

因此,第三步是棘手的部分。使用AJAX,我调用下面看到的deleteChannel(url),它在代码隐藏中调用适当的方法。下面是棘手的部分:

代码语言:javascript
复制
   if (channel != null && channel.Location != null)
    {
         // How do I do this? (Notice that this is code-behind)
         ShowDialogThatAsksUserToConfirm()
    }

代码:ShowDialogThatAsksUserToConfirm()需要调用客户端,说“Location列不为null",然后等待用户说”删除“或”取消“。

我有这样的代码,它将在代码后面调用方法:

代码语言:javascript
复制
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(.)看起来是这样的:

代码语言:javascript
复制
function showDialog(divID) {
$("#" + divID).dialog({
    show: "clip",
    hide: "clip",
    buttons: {
        Ok: function () {
            $(this).dialog("close");
        }
    }
});
}

代码隐藏如下所示:

代码语言:javascript
复制
[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);
                }
            }
  }

以下是视图:

代码语言:javascript
复制
<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>

这只是我的方法,不是最后的,如果有更好的解决办法,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-22 18:31:49

您不能从代码背后调用js,但是ajax方法可以返回询问用户的指示:

代码语言:javascript
复制
if (channel != null && channel.Location != null)
{
   return 'cannot-delete';
}

ajax方法将看到它的成功函数,并提示用户:

代码语言:javascript
复制
$.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中确认,如果允许的话,提交一个强制删除记录。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29805916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档