【问题标题】:ASP.NET mvc validation label on dropdownlist with integer value?带有整数值的下拉列表上的 ASP.NET mvc 验证标签?
【发布时间】:2015-02-19 09:31:52
【问题描述】:

我正在处理一个 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)

我的问题是标题的验证消息显示正常 ("Please enter the title."),但下拉列表的验证是默认的 ("The value 'NaN' is not valid for RegionId.")。

为什么RegionId的验证消息没有显示我在模型("Please select a country.")中设置的消息?是不是因为默认值(OptionLabel)?我该如何解决?

【问题讨论】:

  • 这个问题需要去掉html.dropdownlistfor标签并添加Kendo标签
  • 为什么你还有一个隐藏的属性输入?
  • 是的,抱歉你是对的@StephenMuecke,你可以忽略 hiddenfor。 (答案已编辑)
  • @markpsmith,绝对感谢您的编辑!
  • 没人知道这个话题吗?

标签: c# asp.net asp.net-mvc kendo-ui


【解决方案1】:

您可以将 RegionId 属性更改为 nullable。

[Required(ErrorMessage = "Please select a country.")]
public int? RegionId { get; set; }

【讨论】:

  • 嗨@nevra,谢谢你的回答,但这并不能解决我的问题:-(
  • 这在 .NET Core 2 MVC 和 Telerik UI for AspNet.Core 2018.3.1017 中对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多