我试图使用MVC 4中的Ajax.BeginForm帮助方法将窗体从同步转换为异步.
在我看来,我有:
@{ ViewBag.Title = "Users > Edit"; var options = new AjaxOptions() { Url = Url.Action("Edit","User"),LoadingElementId = "saving",LoadingElementDuration = 2000,Confirm = "Are you sure you want to save this User?" }; } <div id="saving" style="display:none; color:Red; font-weight: bold"> <p>Saving...</p> </div> @using (Ajax.BeginForm(options)) { @Html.ValidationSummary(true) <fieldset> .... FIELDS ... <p> <input type="submit" value="Save" /> </p> </fieldset> }
当点击提交按钮时,正在执行完整的回发.我的动作方法如下所示:
[HttpPost] public ActionResult Edit(User user) { if (ModelState.IsValid) { _repository.Save(user); TempData["message"] = String.Format("{0} has been saved.",user.Username); } return View(user); }
有没有我失踪的东西? MVC 4目前的版本有几个问题吗?
在我的Web.Config我确实有ClientValidationEnabled和UnobtrusiveJavaScriptEnabled设置为true.
我也在我的_Layout.cshtml中指定了这些:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
解决方法
Is there something I’m missing?
也许你错过了不引人注意的ajax脚本:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>