外键字段的显示名称数据注释

Display Name Data annotation for foreign key field

我首先在 entity framework 数据库中为我的外键字段获取正确的显示名称时遇到问题。我遇到了与 here

相同的问题

我的class:

public class CampaignMeta
{

    public long CampaignID;

    [Required]
    [Display(Name = "Campaign Name")]
    public string CampaignName;

    [Display(Name = "Campaign date created")]
    public Nullable<System.DateTime> DateCreated;

    public Nullable<System.DateTime> DateEnded;

    [Display(Name = "Campaign Type")]
    public Nullable<long> CampaignTypeID;

    public virtual CampaignType CampaignType {get;set;}
}

我的一些看法(问题领域):

<div class="form-horizontal">
    <h4>Campaign</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.CampaignName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CampaignName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CampaignName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DateCreated, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DateCreated, new { htmlAttributes = new { @class = "form-control datepicker col-md-2", placeholder = "Enter Drop-off date here..." } })
            @Html.ValidationMessageFor(model => model.DateCreated, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DateEnded, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DateEnded, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DateEnded, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CampaignTypeID, "CampaignTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CampaignTypeID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CampaignTypeID, "", 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>
}

我的外键是 CampaignTypeID,显示的是 CampaignTypeID 而不是 "Campaign Type"。在这种情况下我误解了什么?

请尝试使用以下代码片段。

@Html.LabelFor(model => model.CampaignTypeID, htmlAttributes: new { @class = "control-label col-md-2" })

 @Html.LabelFor(model => model.CampaignTypeID, "Campaign Type", htmlAttributes: new { @class = "control-label col-md-2" })