0
我有以下模型。MVC3編輯模型
public class Person
{
public Guid ID { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Prénom")]
public string FirstName { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Nom")]
public string LastName { get; set; }
[Required]
[DataType("Users")]
[Display(Name = "Adresse")]
public Address Address { get; set; }
正如你所看到的,它包含地址類型的公共字段:
public class Address
{
public Guid ID { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Rue")]
public string Street { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Ville")]
public string City { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Province")]
public string Province { get; set; }
我沒有問題,創建一個新的實例。無論是人與地址貼到數據庫
[HttpPost]
public ActionResult Create(Person model)
{
if (ModelState.IsValid)
{
db.Persons.Add(model);
db.SaveChanges();
我想明白爲什麼當我使用下面的命令數據庫檢索的人時,該地址始終是NULL。
return db.Persons.FirstOrDefault();
感謝
非常感謝Eranga,我今天嘗試了這一點。 – Baral 2012-01-30 13:00:08