如何指定MVC脚手架应显示哪个字段 [英] How do I specify which field should be displayed with MVC Scaffolding

查看:85
本文介绍了如何指定MVC脚手架应显示哪个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF Code First数据库创建MVC4应用程序.我正在处理一些外键声明.我希望使用定义字段来显示在模型声明的支架中的下拉列表中.例如:

I am creating an MVC4 application using EF Code First database. I am working with some foreign key declarations. I wish to use define the field to display in the dropdown in the scaffolding in the model declarations. For instance:

我的简化模型声明如下:

My simplified model declaration is as follows:

public class Contact
{
    public int ID { get; set; }
    public string Prefix { get; set; }
    public string First { get; set; }
    public string Middle { get; set; }
    public string Last { get; set; }
    public string FullName
    {
        get { return (Last + ", " + First + " " + Middle).Trim(); }
    }
}

public class Role
{
    public int ID { get; set; }
    public string RoleName { get; set; }
    public string RoleDescription { get; set; }
}

public class RoleAssignment
{
    [Key]
    public int ID { get; set; }

    [ForeignKey("Contact")]
    public int Contact_ID { get; set; }
    public virtual Contact Contact { get; set; }

    [ForeignKey("Role")]
    public int Role_ID { get; set; }
    public virtual Role Role { get; set; }
}

我生成标准脚手架,编辑.cshtml如下所示:

I generate the standard scaffolding and the edit .cshtml looks like this:

 <fieldset>
    <legend>RoleAssignment</legend>

    @Html.HiddenFor(model => model.ID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Contact_ID, "Contact")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Contact_ID", String.Empty)
        @Html.ValidationMessageFor(model => model.Contact_ID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Role_ID, "Role")
    </div>
    <div class="editor-field">
        @Html.DropDownList("Role_ID", String.Empty)
        @Html.ValidationMessageFor(model => model.Role_ID)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

但是,下拉菜单使用前缀"字段进行下拉菜单和显示.我希望它使用全名"字段.我该如何在Contact模型声明中指定它? (我知道如何修改.cshtml代码,但是我希望它能与纯生成的代码一起使用.)

However, the dropdown uses the "Prefix" field for the drop down and display. I want it to use the "FullName" field. How do I designate this in the Contact model declaration? (I know how to do this my modifying the .cshtml code, but I want it to work with pure generated code.)

推荐答案

确定.想通了.要为您的表指定用于下拉菜单的自定义命名字段,请在模型类上使用"DisplayColumn",然后使用"NotMapped"属性来防止将自定义显示字段映射到数据库,并为其指定一个设置器什么都不做:

OK. Figured it all out. To specify a custom naming field for your table for drop-downs, use the "DisplayColumn" on the model class, and then use the "NotMapped" attribute to prevent the custom display field from being mapped to the database, and give it a setter that does nothing:

[DisplayColumn("FullName")]
public class Contact
{
    public int ID { get; set; }

    [NotMapped]
    [Display(Name = "Full Name")]
    public string FullName
    {
        get { return (Last + ", " + First + " " + Middle).Trim(); }
    }
    public string Prefix { get; set; }
    public string First { get; set; }
    public string Middle { get; set; }
    public string Last { get; set; } }

这篇关于如何指定MVC脚手架应显示哪个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆