MVC下拉名称不会改变

问题描述:

我有一个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/)助手以了解如何正确完成 –

了一个拼写错误,以得到它的工作

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