在MVC 3项目中显示由JsonResult返回的ModelState错误?(Display ModelState error returned by JsonResult in MVC 3 proj

编程入门 行业动态 更新时间:2024-10-25 05:19:09
在MVC 3项目中显示由JsonResult返回的ModelState错误?(Display ModelState error returned by JsonResult in MVC 3 project?)

我有一个使用JsonResult操作而不是ActionResult的创建页面。 在ActionResult操作中,错误显示在违规字段旁边的视图中。 现在JsonResult只返回一个显示在警告框中的字符串。

我可以在视图上显示ModelState错误吗?

控制器

[HttpPost] public JsonResult Create(Tload tload) { if (ModelState.IsValid) { ...save changes return Json(new { Success = 1, TransloadID = transload.TransloadID, ex = "" }); } else { string totalError = ""; foreach (var obj in ModelState.Values) { foreach (var error in obj.Errors) { if (!string.IsNullOrEmpty(error.ErrorMessage)) { totalError = totalError + error.ErrorMessage + Environment.NewLine; } } } return Json(new { Success = 0, ex = new Exception(totalError).Message.ToString()}); }

jQuery / JavaScript代码中的视图

function Save() { // Step 1: Read View Data and Create JSON Object ...do stuff here // Set 2: Ajax Post // Here i have used ajax post for saving/updating information $.ajax({ url: '/Expedite/Create', data: JSON.stringify(salesmain), type: 'POST', contentType: 'application/json;', dataType: 'json', success: function (result) { if (result.Success == "1") { window.location.href = "/Expedite/index"; } else { alert(result.ex); } } }); }

I have a create page that uses a JsonResult action instead of ActionResult. In the ActionResult action, the errors are displayed on the view beside the offending field. Right now the JsonResult only returns a string that is displayed in an alert box.

Can I display the ModelState errors on the view?

The controller

[HttpPost] public JsonResult Create(Tload tload) { if (ModelState.IsValid) { ...save changes return Json(new { Success = 1, TransloadID = transload.TransloadID, ex = "" }); } else { string totalError = ""; foreach (var obj in ModelState.Values) { foreach (var error in obj.Errors) { if (!string.IsNullOrEmpty(error.ErrorMessage)) { totalError = totalError + error.ErrorMessage + Environment.NewLine; } } } return Json(new { Success = 0, ex = new Exception(totalError).Message.ToString()}); }

jquery/javascript code in view

function Save() { // Step 1: Read View Data and Create JSON Object ...do stuff here // Set 2: Ajax Post // Here i have used ajax post for saving/updating information $.ajax({ url: '/Expedite/Create', data: JSON.stringify(salesmain), type: 'POST', contentType: 'application/json;', dataType: 'json', success: function (result) { if (result.Success == "1") { window.location.href = "/Expedite/index"; } else { alert(result.ex); } } }); }

最满意答案

有一个错误的占位符,并最初隐藏它

<div id="err"></div>

以及何时出现错误

else { $("#err").html(result.ex); $("#err").show(); //or you can use .slideDown() etc }

have a placeholder for the error and hide it initially

<div id="err"></div>

and when errors are raised

else { $("#err").html(result.ex); $("#err").show(); //or you can use .slideDown() etc }

更多推荐

本文发布于:2023-04-27 15:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   项目   ModelState   JsonResult   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!