【问题标题】:MVC4: Show error in JQuery Modal when error occurred in controllerMVC4:当控制器发生错误时,在 JQuery 模式中显示错误
【发布时间】:2013-07-25 14:26:21
【问题描述】:

当控制器中发生错误时,我想将部分视图显示为 jQuery 模式。提交表单后,我需要检查验证,如果验证失败,则在 jQuery 模型弹出窗口中显示部分视图。

编辑.cshtml

 <div class="form-actions">
   <button type="submit" class="btn btn-primary" >Save changes</button>
   @Html.ActionLink("Cancel", "Index", new {id=Model.Contact.Number}, new { @class = "btn " })
 </div>

MemberController.cs

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Edit(Activity activity)
 {
    try
    {
        byte[] committeeMemberSpeId = Convert.FromBase64String(activity.Id);
       var committeeMember = db.Committee_Member.FirstOrDefault(x => x.Committee_Member_Id == committeeMemberId);
       if (ValidateEndDate(activity))  //Show here PartialView("ErrorDetail");
       if (ModelState.IsValid)
       {
          if (committeeMember != null)
          {
              ....
              ....
              db.Entry(committeeMember).State = EntityState.Modified;
                        db.SaveChanges();
                        Success("Your information was saved!");
                        return RedirectToAction("Index", new { id = committeeMember.Customer_Number });
            }
        }
        ViewBag.Roles = TempData["Roles"];
        TempData["Roles"] = TempData["Roles"];
        return View(activity);


      }             
      catch (Exception exception)
      {
           Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
           PartialView("ErrorDetail");
      }
  }

我们该怎么做?

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    您可以使用 Ajax 请求发布您的帖子,并将 Json 从操作返回到视图,然后在您的 jquery Ajax 调用的回调中在对话框中显示错误:

     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Edit(Activity activity)
     {
        //Do stuff
    
         return Json(flag, JsonRequestBehavior.AllowGet);
    }
    

    flag 只是一个简单的类,它具有错误和成功字符串/布尔值,用于向用户显示结果。

    public class Flag
    {
        public bool Success { get; set; }//determine whether the call succeeded or not
        public string Error { get; set; }//show some detailed error message
    }
    

    那么在你看来下面的ajax请求:

    function serializeAndSendLoginForm() {
        var form = $("#logOnForm").serialize();
        $.ajax({
            url: '<%: Url.Action("LogOn","Account",new{area="Security"}) %>',
            type: 'POST',
            data: form,
            success: function (data) {
                if (data.Success) {
                   //do more stuff
                } else {
                  showDialog(data.Error);
                }
    
            },
            error: function () {
               alert("error");
            }
        });
    }
    

    加载对话框的函数:

    function showDialog(message){
            var $dialog = $('<div></div>')
                   .html(message)
                   .dialog({
                       autoOpen: false,
                       modal: true,
                       height: 625,
                       width: 500,
                       title: "Login Result"
                   });
            $dialog.dialog('open');
    }
    

    您可以使用某些容器类(而不是我的答案中的标志变量)将其他信息从控制器返回到视图,并向用户显示其他错误详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多