我正在开发一个ASP.NET MVC 4项目,我遇到了为整数字段显示正确的验证标签的问题。
这是我的模型属性:
[Required(ErrorMessage = "Please enter the title.")]
public string Title { get; set; }
[Required(ErrorMessage = "Please select a country.")]
public int RegionId { get; set; }
这是我的cshtml:
@Html.TextBoxFor(m => m.article.Title, new { @placeholder = "Title", @autofocus = true })
@Html.ValidationMessageFor(m => m.article.Title)
@(Html.Kendo().DropDownList()
.Name("RegionControl")
.DataTextField("Name")
.DataValueField("RegionId")
.OptionLabel("Select one...")
.Events(e => e.Change("RegionChange").DataBound("RegionDataBound"))
.BindTo(Model.regions)
.Value(Model.article.RegionId.ToString())
@Html.ValidationMessageFor(m => m.article.RegionId)
我的问题是Title的验证消息显示正常("Please enter the title.")
,但下拉列表的验证是默认值("The value 'NaN' is not valid for RegionId.")
。
为什么RegionId的验证消息不显示我在模型("Please select a country.")
中设置的消息?是由于默认值(OptionLabel)
吗?我该如何解决?
答案 0 :(得分:1)
您可以将RegionId属性更改为nullable
。
[Required(ErrorMessage = "Please select a country.")]
public int? RegionId { get; set; }