加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – Json返回时如何读取modelstate错误?

发布时间:2020-12-15 23:04:27 所属栏目:asp.Net 来源:网络整理
导读:如何显示 JSON返回的ModelState错误? 我想做这样的事情: if (!ValidateLogOn(Name,currentPassword)) { ModelState.AddModelError("_FORM","Username or password is incorrect."); //Return a json object to the javascript return Json(new { ModelStat
如何显示 JSON返回的ModelState错误?

我想做这样的事情:

if (!ValidateLogOn(Name,currentPassword))
    {
        ModelState.AddModelError("_FORM","Username or password is incorrect.");

        //Return a json object to the javascript
        return Json(new { ModelState });
    }

在查看ModelState错误并显示它们时,我的代码必须是什么?

我的实际代码在视图中读取JSON值如下:

function createCategoryComplete(e) { 
    var obj = e.get_object(); 
    alert(obj.Values); 
}

解决方法

如果您要返回JSON,则无法使用ModelState.视图需要的一切都应包含在JSON字符串中.因此,不要将错误添加到ModelState,您可以将其添加到要序列化的模型中:
public ActionResult Index()
{
    return Json(new 
    {
        errorControl = "_FORM",errorMessage = "Username or password is incorrect.",someOtherProperty = "some other value"
    });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读