【发布时间】:2017-07-27 15:37:30
【问题描述】:
我在 Studio 2013 中有一个基于 C# 的 MVC 项目。
我在网页上有许多使用 razor 语法的字段,其中一些只是常规的 @Html.EditorFor(),类为 @class = "form-control",其他是 @Html.DropDownListFor()。
我有一个模型可以让我为表单验证添加数据注释。我正在使用实体框架从 SQL 中引入我的地区,并将这些显示为下拉列表。这一切都很好,这纯粹是显示我拥有的验证消息的问题。
请看一下我用来创建这些的以下代码示例。
这是我的模型...
namespace MySystem.Models
{
[MetadataType(typeof(CRecord_Buddy))]
public partial class CRecord
{
// properties used to pull back and display the name etc instead on an id in dropdown boxes.
[Required(ErrorMessage = "Select District.")]
public string DistrictName { get; set; }
public class CRecord_Buddy
{
[Required(ErrorMessage = "Please enter a Surname.")]
public string Surname { get; set; }
...
}
}
}
我的 create.cshtml 页面如下所示...
//code for Surname
@Html.EditorFor(model => model.client.Surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.client.Surname, "", new { @class = "text-danger" })
@Html.ValidationMessage("Surname", "*", new { @class = "text-danger" })
//code for district
@Html.DropDownListFor(model => model.client.District, new SelectList(Model.allDistricts, "ID", "District1", 1), "- Select District -")
@Html.ValidationMessage("DistrictName", "*", new { @class = "text-danger" })
@Html.ValidationMessageFor(model => model.client.District, "", new { @class = "text-danger" })
下面是验证开始时显示的图像...
从上面的图片可以看出,@Html.EditorFor 项的验证工作正常,而@Html.Dropdownlistfor 项则不行。在填充验证方面,它们的设置相同,但我似乎无法让下拉列表项正常工作。
欢迎提出任何建议。
【问题讨论】:
-
什么是属性
District? (您只显示了一个名为DistrictName)
标签: c# html asp.net-mvc validation