2014-05-05 47 views
9

我被困在ASP.NET MVC5中創建適當的創建/編輯視圖。我有兩個型號DogHuman。一隻狗屬於一個Human。我正在爲Dog創建createedit的下拉列表,讓我可以根據該特定的Dog的名稱選擇Human。這裏是我的模型:爲ASP.NET MVC5關係創建DropDownList

人:

public class Human 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

犬:

public class Dog 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public Human Human { get; set; } 
} 

我創建行動:

// GET: /Dog/Create 
public ActionResult Create() 
{ 
    ViewBag.HumanSelection = db.Humen.Select(h => new SelectListItem 
    { 
     Value = h.ID.ToString(), 
     Text = h.Name 
    }); 
    return View(); 
} 

這裏是我的觀點的相關部分:

<div class="form-group"> 
    @Html.LabelFor(model => model.Human.Name, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(model => model.Human, ViewBag.HumanSelection); 
    </div> 
</div> 

我收到以下錯誤,當我運行此:

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper<Test.Models.Dog>' has no applicable method named 'DropDownListFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. 

我是新的C#&實體框架。我究竟做錯了什麼?有沒有辦法做到這一點,而無需手動查詢數據庫?像Rails中的集合表單助手一樣? 我跟着一堆教程,既舊或太複雜,我不能遵循。

+1

我不認爲你的問題清楚問題是什麼。你是否收到錯誤? – alan

+0

以下連結可能對您有所幫助。此作品適用於我
http://ilyasmamunbd.blogspot.com/2014/03/how-to-create-dynamic-dropdownlist-step.html –

回答

9

需要注意的是,如果您使用DropDownListFor(x => x.Human),則下拉列表的返回值應該是Human對象。

它不是。在您自己的代碼片段中,您將SelectListItem的值設置爲人類的ID。因此,當您提交表格時,您將收到您選擇的ID

以下內容添加到你的模型:

public int HumanId { get; set; } 

綁定您的下拉列表到INT:

@Html.DropDownListFor(model => model.HumanId, (SelectList)ViewBag.HumanSelection); 

現在,當你回到控制器,使用該ID來查找實際你想要的人:

[HttpPost] 
public ActionResult Create (CreateModel model) 
{ 
    if(model.HumanId > 0) 
    { 
     model.Human = GetHumanByID(model.HumanId); 
     //or however you want to get the Human entoty from your database 
    } 
} 

這是一個簡化的解決方案,但我懷疑你的主要困惑源於fa ct表示您希望從DropDownList接收一個Human,而實際上它只會返回一個int(ID)。

編輯

我沒有對你的數據模型的大量信息,但如果你正在使用實體框架,賠率是,你Dog類將有一個名爲HumanId外鍵屬性。如果是這樣的話,你甚至不需要像我之前展示給你的Human實體。如果您將所選ID置於HumanId屬性中,Entity Framework應該能夠使用它來創建您想要的Human/Dog之間的關係。

如果是這樣的話,似乎最好在你的問題中詳細說明這一點,因爲否則這將比實際確認更具猜測性。

編輯2打算在這裏offtopic

您的代碼:

db.Humen 

man的複數形式爲menwomanwomen;但對於human,這是humans :)虎門確實聽起來像一個真棒建議,但;)

+0

1.錯誤表示它沒有沒有這樣的方法。 2.我的db中的相應列被稱爲Human_ID而不是HumanID(就像我遇到的每個教程)。 VS也不會讓我自動完成模型.Human_ID。唯一有效的關鍵是model.Human。 3.表名是由框架自動生成的。 – Prajjwal

+0

1.請詳細說明。拋出異常在哪裏?採取了什麼行動?最好解決你的問題;所以每個人都知道它。 2這不是重要的數據庫欄。如果您使用實體框架,您的代碼與數據庫相對獨立運行。這更多與您的實體類的設置有關。 3.類似的答案,特定的數據庫內容不應與實體框架中的案例相關。 4.關於不讓你完成HumanId; **你需要自己添加該屬性**。只有這樣你才能使用它。 – Flater

+0

另外,不操縱Human_ID col直接打敗使用EF的整點?我來自鐵軌,我認爲它會自動處理這些事情。它自動創建表和Human_ID列就好了。有沒有一種幫助方法可以讓我直接使用對象? – Prajjwal

8

的問題是,你正試圖綁定Human類型的用戶界面,下拉其值是字符串下拉(的ID Human實例),文本也是字符串(實例名稱爲Human)。

您應該綁定到下拉列表,而不是Human的ID,以符合ID被用作值的事實。所以這樣一個觀點模型

public class CreateDogModel 
{ 
    public string Name { get; set; } 

    [Range(0, int.MaxValue)] 
    public int Human { get; set; } 

    public IEnumerable<Human> Humans { get; set; } 
} 

而且GET控制器操作

[HttpGet] 
public ActionResult Create() 
{ 
    var model = new CreateDogModel 
    { 
     Humans = db.Human.ToList() 
    }; 

    return View(model); 
} 

的觀點便成爲

@Html.DropDownListFor(
    model => model.Human, 
    Model.Humans.Select(h => new SelectListItem 
     { 
      Text = h.Name, 
      Value = h.ID.ToString() 
     }), 
    "Please select a Human"); 

在您的文章控制器動作,你現在查找所選擇的人由Human屬性值查看模型

[HttpPost] 
public ActionResult Create(CreateDogModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     // fetch the humans again to populate the dropdown 
     model.Humans = db.Human.ToList(); 
     return View(model); 
    } 

    // create and persist a new dog 

    return RedirectToAction("Index"); 
}