2012-03-18 45 views
1

我是一個新手,所以我想我的問題是微不足道的。asp.net mvc displayFor不顯示子實體值

我用CodeFirst創建了一個簡單的數據庫。我有一個名爲Person的實體,另一個名爲LocationPerson有一個名爲LocationIdForeignKey屬性和Location類型的另一個屬性以供參考。

我創建了一個視圖,其中有一個窗體可以用來創建新的Person記錄。

其中一個表單域是一個人的'位置'的DropDownList。它需要一個Id值並將其分配給PersonLocationId屬性。

該視圖引用create控制器操作,該操作接收Person作爲參數,並使用person屬性綁定到表單字段。然後,它執行SaveChanges()DBContext,並將其重定向到具有特定Person'Id`的名爲details的另一個Action。

Details在接收到的Id上產生一個find,並將結果(Person)發送到視圖進行展示。

這是我的問題:
我嘗試顯示我通過的特定'Person'的位置txt,並且什麼也沒得到。 我這樣做:

@Html.DisplayFor(model => model.Location.LocationValue) 

模型當然是一個'人'。 爲什麼我對此沒有任何價值?

下面的代碼:

public class Person 
    { 
     public int PersonId { get; set; 
     public string LocationId { get; set; } 
     ... 
     public virtual Location Location { get; set; } 
    } 

    public class Location 
     { 

      public int LocationId { get; set; } 
      public string LocationValue { get; set; } 

      public virtual ICollection<Person> Persons { get; set; } 
     } 

    public ActionResult Detailes(int id) 
    { 
     Person person = db.Persons.Find(id); 

     return View(person); 
    } 

    [HttpPost] 
    public ActionResult AddPerson(Person person) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Persons.Add(person); 
      db.SaveChanges(); 
      return RedirectToAction("Detailes", new {id = person.PersonId}); 
     } 
     return View(GetAddPersonViewModel()); 
    } 

這裏的模型是形式的視圖模型

@Html.DropDownListFor(model => model.Person.LocationId, Model.LocationsList) 

再次,在 '細節' 觀點的陳述:

 @Html.DisplayFor(model => model.Location.LocationValue) 

謝謝。

+0

什麼是位置值的類型?你爲什麼不使用'Html.TextBoxFor'? – gideon 2012-03-18 10:57:40

+0

如果您發佈定義外鍵的Person/Location類和配置類將有助於確定問題 – Jayanga 2012-03-18 11:15:00

+0

'LocationValue'是字符串類型。它擁有該位置的名稱。 TextBox是用於輸入,我只需要一個它的值的演示文稿。 – 2012-03-18 11:16:44

回答

0

如果您尚未設置任何配置來映射Person和Location之間的關係,則可能會發生這種情況。這裏是一個示例配置類

public class LocationConfiguration : EntityTypeConfiguration<Location> 
    { 
     public LocationConfiguration() 
     { 
      HasKey(a => a.Id); 
      HasMany(location => location.Persons).WithOptional(person => person.Location). 
       HasForeignKey(person => person.LocationId); 
     } 
    } 

與此配置添加到您的上下文如下

public class YourContext : DbContext 
    { 

     // your DBSets and contructors, etc 


     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Configurations.Add(new LocationConfiguration());    
      base.OnModelCreating(modelBuilder); 
     } 


    } 

問候

+0

謝謝。真相是我很喜歡MvcMusicStore教程。如果你看那裏,就不需要地圖配置......你也可能會注意到,從DB的圖表中可以看出這些連接是自解釋的。 – 2012-03-18 11:45:50

+0

試試這個,並檢查你的程序是否工作。如果這有幫助,請將此標記爲答案:) – Jayanga 2012-03-18 11:50:24

+0

我做到了。我得到了這個錯誤消息:\ tSystem.Data.Entity.Edm.EdmAssociationConstraint::參照約束的從屬角色中的所有屬性的類型必須與主體角色中相應的屬性類型相同。實體'Person'上的屬性'LocationId'的類型與參照約束'Location_Persons'中的實體'Location'上的屬性'LocationId'的類型不匹配。紅色標記的行是「行32:@ Html.DropDownListFor(model => model.Person.LocationId,Model.LocationsList)」 – 2012-03-18 12:00:25