How do I specify which field should be displayed with MVC Scaffolding

六眼飞鱼酱① 提交于 2019-12-24 12:14:12

问题


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; }
}

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>

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.)


回答1:


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; } }



回答2:


I have a partial answer to my question:

Adding the "DisplayColumn" attribute to the model class allows you to change the display field for the scaffolding as follows:

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

However, it does not work with the read only field "FullName". Still trying to figure that one out...



来源:https://stackoverflow.com/questions/24589297/how-do-i-specify-which-field-should-be-displayed-with-mvc-scaffolding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!