我正在处理一个 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' 对 RegionId 无效。")
。
为什么 RegionId 的验证消息没有显示我在模型中设置的消息 ("Please select a country.")
?是不是因为默认值(OptionLabel)
?我该如何解决?
最佳答案
您可以将 RegionId 属性更改为 nullable
。
[Required(ErrorMessage = "Please select a country.")]
public int? RegionId { get; set; }
关于c# - 具有整数值的下拉列表上的 ASP.NET mvc 验证标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602783/