asp.net MVC ModelState.IsValid返回false [英] asp.net MVC ModelState.IsValid returning false

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

问题描述

我正在ASP.NET MVC应用程序中工作.我有一个如下的视图模型:

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; }
}

在UI透视图中,用户可以输入日期作为mmddyyyy.当用户以这种格式输入时 01012001,我在控制器中的ModelState.IsValid代码段返回false.当我在ModelState中进行快速监视时,看到一个属性"SampleDate"的错误,说值01012001对SampleDate无效".

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".

在我的Modelbinder中,在OnModelUpdated事件期间,我试图将值01012001格式化为01/01/2001,然后将其分配回SampleInterestViewModel.SampleDate,以为ModelState.IsValid可能返回true而没有该错误.但是ModelState.IsValid仍然是false,当我查看ModelState字典时,这个特定属性在其集合中仍然存在该错误.

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.

最后,我尝试格式化01012001并将值01/01/2001直接更新为ModelState词典中的Property SampleDate.但是ModelState.IsValid仍然为false,显示了SampleDate属性的相同错误.无法弄清楚为什么ModelState.IsValid可以工作,以及如何以及何时将其设置为false.

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.

如果用户在UI中输入01012001,我仍然需要在modelbinder中将其格式化为01/01/2001,并确保ModelState.IsValid为true,以便控制器的其余代码可以按预期工作.在用户界面中,我正在执行AjaxSubmit来发布sampleDate值.

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.

任何想法或评论.

推荐答案

我可以看到一些解决方案:

I can see a few solutions:

  1. 在将格式绑定器中的格式固定后,可以使用ModelState["SampleDate"].Errors.Clear();清除ModelState错误.
  2. 使用JavaScript验证输入,以强制用户输入正确的格式.
  3. 在表单发布前使用JavaScript更改用户的格式.
  1. After fixing the format in your model binder, you could clear the ModelState error with ModelState["SampleDate"].Errors.Clear();.
  2. Use JavaScript to validate the input to force the user to enter the correct format.
  3. Use JavaScript to change the format for the user before form post.

我认为1和2的组合是最好的选择.

I think a combination of 1 and 2 is your best bet.

这篇关于asp.net MVC ModelState.IsValid返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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