0

我的 asp.net mvc 应用程序中有一个简单的 ViewModel 类。

public class DestinationViewModel
{
     [Display(Name = "Country")]
     [some validation attributes]
     public string CountryName { get; set; }

     [Display(Name = "Destination")]
     [some validation attributes]
     public string DestinationName { get; set; }

     public DateTime? DepartureDate { get; set; }

     public DateTime? ArrivalDate { get; set; }
}

当我将数据传递给控制器​​时,我的应用程序显示无效出发和到达日期的验证错误。但我不想要这样的行为。有什么方法(属性)可以仅在DepartureDateArrivalDate字段上禁用验证?

4

2 回答 2

2

The MVC data binder will attempt to convert which ever value you put in your input field to it's corresponding ViewModel type. In your scenario, you are passing "21321" which obviously isn't going to work as it's not valid Date/Time.

Either pass a valid DateTime string value or leave it empty (as the field is nullable).

Tip - It's usually a good idea from a users point of view to make date fields readonly and provide a date picker of some sort to avoid issues with manually entering valid date formats.

于 2012-10-22T15:40:26.987 回答
1

如果你想写任何你想要的东西,那么使用string,不DateTime?使用或使用以下解决方案之一:

于 2012-10-22T15:46:07.873 回答