2013-10-04 85 views
1

我一直在這個問題上停留了一段時間。 我想從另一個表中獲取並顯示來自外部表或主表的信息。顯示ForeignKey信息頁

例如,我有一個人和寵物表。

public class Person 
{ 
    public int id { get; set; } 
    // rest of the fields here 
} 

public class Pet 
{ 
    [DisplayName("Belongs to:")] 
    public int person_id { get; set; } 
    // Rest of the fields here 
} 

person_id是一個外鍵。

這是我的看法

@model SpamValley.Models.Pet 

    @{ 
     ViewBag.Title = "Create"; 
    } 

    <h2>Create</h2> 

    @using (Html.BeginForm()) { 
     @Html.ValidationSummary(true) 

     <fieldset> 
      <legend>Pet</legend> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.pet_name) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.pet_name) 
       @Html.ValidationMessageFor(model => model.pet_name) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.pet_type) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.pet_type) 
       @Html.ValidationMessageFor(model => model.pet_type) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.person_id, "Person") 
      </div> 
      <div class="editor-field"> 
      @if (Model == null || Model.person_id == 0) 
      { 
       Html.DropDownList("person_id", "Select the person this pet belongs to"); 
      } 
      else 
      { 
       @Html.DisplayFor(M => M.person_id); 
      } 
      @Html.ValidationMessageFor(model => model.person_id) 
      </div> 

      <p> 
       <input type="submit" value="Create" /> 
      </p> 
     </fieldset> 

} 

控制器:

[HttpGet] 
[DisplayName("Create")] 
public ActionResult Create() { return Create_Get(0); } 

public ActionResult Create_Get(int p_id) 
{ 
    if (p_id == 0) 
    { 
     ViewBag.person_id = new SelectList(db.People, "id", "first_name"); 
     return View(); 
    } 
    else 
    { 
     // Person Ps = db.People.ToList().Single(Per => Per.id == p_id); 
     // ViewBag.person_id = Ps.first_name + " " + Ps.last_name; 

     Pet P = new Pet { id = p_id }; 
     return View(P); 
    } 
} 

現在我知道有幾件事情錯了上面的代碼,但我更擔心的是如何從其它表中顯示的信息。例如:我想顯示Pets.Create View上的人物的名字。我還想在Person.Index視圖中顯示Pets.Name。

我可以很容易地在SQL數據庫上做到這一點,但我對這個mvc邏輯有點困惑。

任何幫助,非常感謝。

+0

不是Pet類有一個名爲Person的屬性返回它所屬的人嗎? Person類應該有一個名爲Pets的屬性,其中包含他所有的寵物 –

回答

0

首先,向Person添加一個集合屬性以持有該人的所有寵物,並將屬性添加到Pet以擁有該寵物的所有者。

public class Person 
{ 
    public int id { get; set; } 
    // rest of the fields here 

    public virtual ICollection<Pet> Pets { get; set; } 
} 

public class Pet 
{ 
    [DisplayName("Belongs to:")] 
    public int person_id { get; set; } 
    // Rest of the fields here 

    public virtual Person Owner { get; set; } 
} 

其次,您可能需要使用Entity Framework's fluent API做一些小配置。

第三,編輯您的視圖以利用您的新屬性。例如,要顯示寵物主人的姓名:

@Html.DisplayFor(model => model.Owner.Name) 
+0

謝謝!這解決了我的一個問題。順便說一句,我不太瞭解MVC邏輯,但這是從數據庫中讀取的最佳方式嗎?我有這個私人的SpamValleyEntities db =新的SpamValleyEntities();它將連接到數據庫,但我覺得它正在爲每個請求建立一個新的連接,並且可能會很慢。有什麼像線程/連接池我可以做什麼來加快速度或是MVC的工作方式? – JohnC1

+0

@ JohnC1,我從事的MVC項目沒有大量的用戶,但我可以說我從來沒有爲每個請求創建新的DbContext時遇到問題。如果您搜索StackOverflow,則應該在該主題上找到很多問題(例如http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why/10588594#10588594)。 – devuxer