【问题标题】:Show alert after action in mvc3/4在 mvc3/4 中操作后显示警报
【发布时间】:2013-05-16 15:26:20
【问题描述】:

我需要通知用户点击操作的成功/失败。在视图中,我准备了进入控制器、执行数据库操作并返回结果的操作链接。然后我想通过消息“完成”或“失败”显示警报。一切都应该在不重新加载页面的情况下完成。我试图定义 @Ajax.ActionLink 和 text/javascript 函数,但它根本不起作用......请帮忙。提前致谢。

罗伯特

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-ajax alert


    【解决方案1】:

    首先,您应该包含脚本:jquery 和 jquery.unobtrusive-ajax,例如:

    <head>
        ...
        <script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    </head>
    

    内部视图:

    <script type="text/javascript">
        function myCallback(data) {
            if(!data.Success)
            {
                 alert(data.ErrorMessage);
            }
            else
            {
                alert("id: " + data.Id);
            }
        }
    </script>
    
    @Ajax.ActionLink("Actionlink", // <-- Text to display
                     "GetDeneme", // <-- Action Method Name
                     new { id = "2"},
                     new AjaxOptions
                     {                         
                         HttpMethod = "POST", // <-- HTTP method
                         OnSuccess = "myCallback"
                     })
    

    在控制器中:

    public ActionResult GetDeneme(string id)
            {
                var error = (id == "3");
    
                if (error)
                {
                    return Json(new { Success = false, ErrorMessage = "Error!" });
                }
    
                return Json(new { Success = true, Id = id });
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2015-10-08
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      相关资源
      最近更新 更多