2015-09-25 41 views
0

我有一個ASP.NET MVC局部視圖,但我想改名字,否則綁定失敗MVC下拉名稱不會改變

@Html.DropDownListFor(
    model => model.RoomTypeID, 
    Model.RoomTypes 
     .Select(x => new SelectListItem() 
     { 
      Text = x.Name, 
      Value = x.ID.ToString() 
     }), 
    new 
    { 
     @class = "span12", 
     id = "Features_" + Model.GeneratedID + "__RoomTypeID", 
     name = "Featurs[" + Model.GeneratedID + "].RoomTypeID" 
    }) 

如果我跑我的軟件,並添加新項目(使用)的GUID被產生,因爲它應該EXCEPT名這個局部視圖現在將代替RoomTypeID的Features[guid].RoomTypeID

如果我去做檢查元素和我編輯名稱,Features[guid].RoomTypeID然後結合的作品和我的名單不爲空

我試過了將其更改爲

@Html.DropDownList(
    "Featurs[" + Model.GeneratedID + "].RoomTypeID", 
    new SelectList(Model.RoomTypes, "ID", "Name", Model.RoomTypeID), 
    "-- New Address --", 
    new { @class = "span12", id = "Features_" + Model.GeneratedID + "__RoomTypeID" }) 

但是結合隨後也failse

編輯1: 看來我在管理的第二個

+1

似乎你在分配姓名時出現了錯字,應該是「特徵」而不是「特技」? –

+0

是啊,剛剛注意到,當我再次查看我的代碼 –

+0

您可能需要查看[BeginCollectionItem](https://www.nuget.org/packages/BeginCollectionItem/)助手以瞭解如何正確完成 –

回答

0

了一個拼寫錯誤,以得到它的工作

 @Html.DropDownList("Features[" + Model.GeneratedID + "].RoomTypeID",new SelectList(Model.RoomTypes, "ID", "Name", Model.RoomTypeID),"-- Select Feature --", new { @class = "span12", id = "Features_" + Model.GeneratedID + "__RoomTypeID" })