C#.NET MVC3 ModelState.IsValid [英] C# .NET MVC3 ModelState.IsValid

查看:512
本文介绍了C#.NET MVC3 ModelState.IsValid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用JSON张贴从形式和ModelState.isValid()返回虚假数据,我把的WriteLine所有传入的数据,一切都看起来不错的数据明智的,有显示模式状态的错误要弄清楚什么是路没有验证?
这个确切的code正常工作与其他型号

Im using JSON to post data from a form and ModelState.isValid() returning false, i put a WriteLine for all incoming data and everything looks fine data wise, is there a way to display model state errors to figure out what is not validating? this exact code works fine with other models

[HttpPost]
public ActionResult mobileCreateAction(Trip trip)
{
    if (ModelState.IsValid)
    {
        System.Diagnostics.Debug.WriteLine("saving");
        DB.Trips.Add(trip);
        DB.SaveChanges();
        return Json(new
        {
            success = true,
            msg = "Success saving trip"
        });
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("invalid model state");
        return Json(new
        {
            success = false,
            msg = "Error saving trip"
        }, JsonRequestBehavior.AllowGet);
    }  
}

感谢

推荐答案

要获得模型状态错误列表:

To get a list of errors in the model state:

var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();

再穿上这条线一个破发点,并检查错误变量。它会给你的属性列表与他们各自的错误,你的模型。

Then put a break point on this line and inspect the errors variable. It will give you a list of properties on your model with their respective errors.

这篇关于C#.NET MVC3 ModelState.IsValid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆