0

I am new to MVC and was trying to get the Scaffolding to automagically create the drop down list in the PersonTitle for Person and Title. I got the drop down list to work however, the scaffolding function takes the first instance of a string as the value of the drop down. As in the case of the Person, the first name is used. However, that is not very informative.

  1. I want to be able to modified it to contain the First and Last name. What is the best solution for this? I tried adding a FullName field but that has its own set of problems. Once of them being, I don't want the program/user to handle extra data entries.

  2. Is there a better method of creating the drop down list for Person and Title after the scaffolding completes? What is the best way to add foreign key drop down list from other tables?

It would help if you provide code samples as I am new to MVC and the best practices solution as I would have to do this to several other tables.

Thanks much.

A person can have many titles // Title -->>PersonTitle<<<----Person

public class PersonTitle
{
    //person can have many titles
    public int PersonTitleID { get; set; }

    public int PersonID { get; set; }
    public int TitleID { get; set; }


    public virtual Person Person { get; set; }
    public virtual Title Title { get; set; }


}

List of Titles

public class Title
{
    public int TitleID { get; set; }

    [Display(Name = "Title Name")]
    public string TitleName { get; set; } //drop down text


    public virtual ICollection<PersonTitle> PersonTitles { get; set; }
}

List of People

public class Person
{

    public int PersonID { get; set; }

    public string FirstName { get; set; } //drop down text is used
    public string LastName { get; set; }

    public virtual ICollection<PersonTitle> PersonTitles { get; set; }

}
wirble
  • 151
  • 1
  • 3
  • 16
  • can help me understand why you have PersonTitle and Title classes? There is possibility of inheritance here. – renakre Apr 07 '15 at 01:23
  • @erkaner I just modeled it how I would normally modeled it in the database. The PersonTitle class has many other fields, but was simplified here also. – wirble Apr 07 '15 at 01:54
  • one more question: it says `//person can have many titles` but you defined it as `public virtual Title Title { get; set; }` I think this is not okay ? – renakre Apr 07 '15 at 02:31
  • I followed these examples [sample1](http://stackoverflow.com/questions/15153390/many-to-many-mapping-with-extra-fields-in-fluent-api), [sample2](http://stackoverflow.com/questions/5434125/entity-framework-codefirst-many-to-many-relationship-with-additional-information), and [sample3](http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table). I have seen other examples that is a little different but it doesn't allow adding additional properties/fields. – wirble Apr 07 '15 at 05:22
  • @erkaner this works as intended for the drop down of Person and Title. So each row of PersonTitle has unique properties and each Person and Title. Whereas each person has a collection of Titles. I am trying to wrap my head around the concept of why EF needs these to map it, but it seems to work. – wirble Apr 07 '15 at 05:37

0 Answers0