modelstate.isvalid false

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了modelstate.isvalid false相关的知识,希望对你有一定的参考价值。

http://stackoverflow.com/questions/1791570/modelstate-isvalid-false-why

第一个

About "can it be that 0 errors and IsValid == false": here\'s MVC source code fromhttp://aspnet.codeplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266501

public bool IsValid {
    get {
        return Values.All(modelState => modelState.Errors.Count == 0);
    }
}

Now, it looks like it can\'t be. Well, that\'s for ASP.NET MVC v1.

 

 

第二个

As you are probably programming in Visual studio you\'d better take advantage of the possibility of using breakpoints for such easy debugging steps (getting an idea what the problem is as in your case).

Just place them just in front / at the place where you check ModelState.isValid and hover over the ModelState.

Now you can easily browse through all the values inside and see what error causes the isvalid return false.

可以直接用lambda表达式把错误筛选出来ModelState.Values.Select(x=>x.Errors).Where(x=>x.Count>0)

 

以上是关于modelstate.isvalid false的主要内容,如果未能解决你的问题,请参考以下文章

当模型具有外键时,ModelState.IsValid 为 false

c# -ModelState.IsValid 仅在资源语言更改时返回 false?

尝试保存实体时 ModelState.isvalid 为 false,错误表示相关实体中需要字段

EmailAddress DataAnnotation 在 View 上标记为有效但 ModelState.IsValid = false?

modelstate.isvalid false 因为该项目始终为空

为啥我的 MVC 应用程序中的 ModelState.IsValid 属性在我看来一切正常时等于 false