我正在尝试进入 MVC3 的第一步,并且遇到了将模型中的导航属性转换为视图的问题。似乎在视图中导航属性不允许客户端验证,也没有选择“显示”标签属性。
我有以下简单模型:
public class Entity
{
[Key,
ScaffoldColumn(false)]
public int Entity_Id { get; set; }
[Display(Name = "Entity Name"),
Required(ErrorMessage = "Please enter the entity name."),
StringLength(150, ErrorMessage = "Please ensure that the entity name is under 150 characters.")]
public string Entity_Nm { get; set; }
[Display(Name = "Entity Type"),
Required(ErrorMessage="Please select the entity type"),
ForeignKey("EntityType")]
public int EntityType_Id { get; set; }
public virtual EntityType EntityType { get; set; }
}
其中引用了这个模型:
public class EntityType
{
[Key]
public int EntityType_Id { get; set; }
[Display(Name = "Entity Name"), Required(ErrorMessage="Please enter the entity type name.")]
public string EntityType_Nm { get; set; }
}
当我为此模型创建具有读/写操作和视图的控制器时,我得到以下创建表单:
<fieldset>
<legend>Entity</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Entity_Nm)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Entity_Nm)
@Html.ValidationMessageFor(model => model.Entity_Nm)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EntityType_Id, "EntityType")
</div>
<div class="editor-field">
@Html.DropDownList("EntityType_Id", String.Empty)
@Html.ValidationMessageFor(model => model.EntityType_Id)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
除了实体类型下拉列表的标签之外,这很好,由于某种原因,它没有拾取模型中导航属性的“显示”属性(注意缺少空格)。尽管使用“必需”属性装饰了属性,但也没有为下拉列表启用客户端验证(服务器端验证没有问题)。客户端验证适用于其他字段。请注意,所有必需的 .js 脚本文件都已包含在内,并且我还在 web.config 中添加了相关的启用验证密钥。
有什么想法我在这里想念的吗?谢谢大家。