我目前在ASP.NET MVC5项目中有以下模型:
ComponentModel.cs
public class Component
{
public int Id { get; set; }
...
[Required]
[Display(Name="Equipment Name")]
public int EquipmentId { get; set; }
[Required]
[Display(Name = "Motor Name")]
public int MotorId { get; set; }
[Required]
[Display(Name = "Gearbox Name")]
public int GearboxId { get; set; }
public virtual Equipment Equipment { get; set; }
public virtual Motor Motor { get; set; }
public virtual Gearbox Gearbox { get; set; }
}
MotorModel.cs(电机,变速箱和设备型号的格式相同)
public class Motor
{
public int Id { get; set; }
...
public virtual ICollection<Component> Components { get; set; }
}
但是,使用scaffolding生成控制器时,正确显示数据库表中的可选值,但名称显示为EquipmentId,MotorId和GearboxId,字段也不会根据需要进行验证。
我缺少什么才能使显示名称正确并且字段按预期验证?
更新1 - 创建视图
....
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Component</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ImageUri, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ImageUri, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ImageUri, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EquipmentId, "EquipmentId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("EquipmentId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EquipmentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MotorId, "MotorId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("MotorId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MotorId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.GearboxId, "GearboxId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("GearboxId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.GearboxId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}