我想在MVC4(實體框架,代碼優先)創建下拉菜單。我的目標是在課堂上準備價值,所以我不需要在控制頁面上額外編程。下拉菜單與ASP.NET MVC 4和EF
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我可以下拉菜單與價值觀 「名姓」,但我想「Prefix.Name名姓」。例如「Mr John Doe」等。當前代碼返回錯誤:
Object reference not set to instance of object.
如何做到這一點?
謝謝你的建議。 – eslopes 2013-02-26 21:50:08
歡迎。你試過了嗎? – 2013-02-27 04:50:02