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

时间:2015-09-28 19:16:37

标签: asp.net-mvc entity-framework data-annotations

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

相同

我的课程:

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,而不是&#34;广告系列类型&#34;。在这样的场景中我有什么误解?

1 个答案:

答案 0 :(得分:4)

请尝试使用以下代码段。

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

OR

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