大佬教程收集整理的这篇文章主要介绍了jQuery验证,ASP.NET MVC ModelState错误(异步POST),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的行动是这样的:
[httpPost] public ActionResult SaveCustomer(Customerviewmodel input) { if (!ModelState.IsValid) { // <-- business validation return Json(new { success = false,errors = ???}); } // persist return Json(new { success = true }); }
在我看来,如何用jquery验证这个错误?
如果可以发布一些代码进行抽样…我会加以考虑!
多谢你们!
所以:
<div id="myform"> @Html.Partial("_MyForm") </div>
然后在_MyForm.cshtml里面:
@model Customerviewmodel @using (Html.beginForm()) { @Html.EditorFor(x => x.Foo) @Html.ValidationmessageFor(x => x.Foo) <br /> @Html.EditorFor(x => x.bar) @Html.ValidationmessageFor(x => x.bar) <br /> <input type="submit" value="OK" /> }
并且控制器动作将成为:
[httpPost] public ActionResult SaveCustomer(Customerviewmodel model) { if (!ModelState.IsValid) { return PartialView("_MyForm",model); } return Json(new { success = true }); }
最后一步是AJAXify这个表单可以在一个单独的javascript文件中完成:
$(function () { $('#myform').delegate('form','submit',function () { $.ajax({ url: this.action,type: this.method,data: $(this).serialize(),success: function (result) { if (result.success) { // We have a JSON object in case of success alert('success'); } else { // We have the partial with errors in case of failure // so all we have to do is update the DOM $('#myform').html(result); } } }); return false; }); });
以上是大佬教程为你收集整理的jQuery验证,ASP.NET MVC ModelState错误(异步POST)全部内容,希望文章能够帮你解决jQuery验证,ASP.NET MVC ModelState错误(异步POST)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。