我有个模特。
看上去像这样
public class AddEditNotificationViewModel : BaseViewModel
{
public TimeSpan TimeOfDayToRun { get; set; }
public string[] DaysRunning { get; set; }
public string Id { get; set; }
public string CampaignId { get; set; }
public int ForHowManyWeeks { get; set; }
public DateTimeOffset StartDate { get; set; }
public bool RunForever { get; set; }
public string TimeZone { get; set; }
}在我看来,我有这个@Html.ValidationSummary( false )。
它显示了3个属性的错误,但我对它们没有必需的属性。
看上去很诡异?
发布于 2015-04-21 11:35:16
默认情况下,值类型(如bool和int)被认为是必需的,除非使用Nullable<T>类型。
在您的模型中,TimeOfDayToRun、ForHowManyWeeks和RunForever是非空值类型。如果您将TimeOfDayToRun类型替换为Nullable<TimeSpan>或TimeSpan?,您将告诉MVC绑定器允许空值,并且不会出现任何验证错误。
尝试以下模式:
public class AddEditNotificationViewModel : BaseViewModel
{
public TimeSpan? TimeOfDayToRun { get; set; }
public string[] DaysRunning { get; set; }
public string Id { get; set; }
public string CampaignId { get; set; }
public int? ForHowManyWeeks { get; set; }
public DateTimeOffset StartDate { get; set; }
public bool? RunForever { get; set; }
public string TimeZone { get; set; }
}https://stackoverflow.com/questions/29771022
复制相似问题