我正在尝试在 MVC4(实体框架,代码优先)中创建下拉菜单。我的目标是在课堂上准备价值,所以我不需要在控制页面上进行额外的编程。
public class Prefix
{
[Key]
public int PrefixID { get; set; }
[StringLength(20)]
public string PrefixName { get; set; }
}
public class People
{
[Key]
public int PersonID { get; set; }
[StringLength(20)]
public string FirstName { get; set; }
[StringLength(20)]
public string LastName { get; set; }
[ForeignKey("PrefixID")]
public Prefix Prefix { get; set; }
[Required]
public int PrefixID { get; set; }
public string FullName // this isn't working
{
get
{
return Prefix.PrefixName +" " + FirstName + " " + LastName;
}
}
}
public class PersonSalary
{
[Key]
public int SalaryId { get; set; }
//...
[ForeignKey("EmployeeID")]
public Person HEmployee {get;set;}
public int EmployeeID { get; set; }
}
SalaryController 中的代码:
ViewBag.EmployeeID = new SelectList(db.People, "PersonID", "FullName", personsalary.EmployeeID);
在 SalaryController 我可以得到带有值 "Name Surname" 但我想要"Prefix.Name Name Surname"的下拉菜单。例如“Mr John Doe”等。当前代码返回错误:
对象引用未设置为对象实例。
怎么做?