asp.net MVC ModelState.IsValid 返回 false
我正在开发 ASP.NET MVC 应用程序。我有一个视图模型如下:
public class SampleInterestViewModel
{
//Properties defined
//One such property that shows an error in ModelState is as follows
public DateTime? SampleDate { get; set; }
}
从 UI 角度来看,用户可以输入日期作为 mmddyyyy。当用户以这种格式输入时说 01012001,控制器中的 ModelState.IsValid 代码段返回 false。当我在 ModelState 中进行快速观察时,我看到属性“SampleDate”出现错误,显示“值 01012001 对于 SampleDate 无效”。
在我的 modelbinder 中,在 OnModelUpdated 事件期间,我尝试将值 01012001 格式化为 01/01/2001 并将其分配回 SampleInterestViewModel.SampleDate,认为 ModelState.IsValid 可能会返回 true 而不会出现该错误。但 ModelState.IsValid 仍然是 false,当我查看 ModelState 字典时,这个特定属性在其集合中仍然存在该错误。
最后,我尝试格式化 01012001 并将值 01/01/2001 直接更新为 ModelState 字典中的 Property SampleDate。但 ModelState.IsValid 仍然为 false,显示 SampleDate 属性的相同错误。无法弄清楚为什么 ModelState.IsValid 有效以及如何以及何时将其设置为 false。
如果用户在 UI 中输入 01012001,我仍然需要在 modelbinder 中将其格式化为 01/01/2001 并确保 ModelState.IsValid 为 true,以便控制器代码的其余部分可以按预期工作。在 UI 中,我正在执行 AjaxSubmit 来发布sampleDate 值。
任何想法或评论。
I am working in an ASP.NET MVC Application. I have a view model as follows:
public class SampleInterestViewModel
{
//Properties defined
//One such property that shows an error in ModelState is as follows
public DateTime? SampleDate { get; set; }
}
From UI Perspective user can enter date as mmddyyyy. And when user enters in such format say
01012001, my ModelState.IsValid code piece in controller returns false. When I did a quick watch in ModelState, I see an error for the propery "SampleDate", saying "The Value 01012001 is not valid for SampleDate".
In my modelbinder, during the OnModelUpdated event I tried to format the value 01012001 to 01/01/2001 and assigned it back to SampleInterestViewModel.SampleDate thinking that ModelState.IsValid might return true without that error. But still ModelState.IsValid is false and I when I looked in to the ModelState dictionary, this particular property still has that errors in its collection.
Lastly I tried to format 01012001 and update the value 01/01/2001 directly to the Property SampleDate in the ModelState dictionary. But still ModelState.IsValid is false showing the same error for the SampleDate property. Can't figure out why ModelState.IsValid works and how and when it gets set to false.
If the User enter 01012001 in the UI, I still need to format it in the modelbinder to 01/01/2001 and make sure that ModelState.IsValid it true so that the rest of my controller code can work as expected. In the UI I am doing an AjaxSubmit to post the sampleDate value.
Any thoughts or comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以看到一些解决方案:
ModelState["SampleDate"].Errors.Clear();
清除 ModelState 错误。我认为 1 和 2 的组合是最好的选择。
I can see a few solutions:
ModelState["SampleDate"].Errors.Clear();
.I think a combination of 1 and 2 is your best bet.
尝试在您的操作中使用要绑定的参数白列表,
并在该列表中省略您自己绑定的参数。阅读此 ScottGu 的帖子以获取更多信息。
Try using in your action a white list of parameters to bind, with
And in that list, ommit the parameter that you are binding by yourself. Read this ScottGu's post for more information.