Asp.net 外键积垢显示名称

Asp.net 外键积垢显示名称,asp.net,entity-framework,asp.net-mvc-5,Asp.net,Entity Framework,Asp.net Mvc 5,我目前在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 Na

我目前在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(电机、齿轮箱和设备型号的格式相同)

公共级电机
{
公共int Id{get;set;}
...
公共虚拟ICollection组件{get;set;}
}
但是,当使用scaffolding生成控制器时,is正确显示数据库表中的可选值,但名称显示为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")
}
。。。。
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
组成部分

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”}) @LabelFor(model=>model.Description,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Description,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Description,“,new{@class=“text danger”}) @LabelFor(model=>model.Location,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Location,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Location,“,new{@class=“text danger”}) @LabelFor(model=>model.ImageUri,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ImageUri,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ImageUri,“,new{@class=“text danger”}) @LabelFor(model=>model.EquipmentId,“EquipmentId”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“EquipmentId”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.EquipmentId,“,new{@class=“text danger”}) @LabelFor(model=>model.MotorId,“MotorId”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“MotorId”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.MotorId,“,new{@class=“text danger”}) @LabelFor(model=>model.GearboxId,“GearboxId”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“GearboxId”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.GearboxId,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
能否提供生成的视图文件的示例?@StephenReindl-已更新以显示视图。谢谢:)在视图中更改标签是有效的(正如预期的那样),但是这仍然无法修复验证。单击表单上的提交将验证除设备、齿轮箱和电机下拉列表之外的所有字段。如果输入操作并使用调试器跟踪代码,是否填写了设备ID、电机ID和齿轮箱ID的值?
....    

@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")
}