微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何显示 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"
    });
}

原文地址:https://www.jb51.cc/aspnet/249944.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐