ASP.NET MVC 3绑定不成功

时间:2012-09-07 15:53:36

标签: asp.net-mvc

我无法将我的ASP.NET MVC 3 View数据绑定回模型。

string ContactNamestring Title等典型对象正在成功发回。

但是我的public List<KeyValuePair<string, ListingTranslation>> ExpirimentToRemove对象由于某种原因没有被发回。

以下内容包含未成功绑定的特定对象的代码。

<div>
     <ul class="nav nav-tabs">
        @for (int i = 0; i < Model.ExpirimentToRemove.Count; i++)
        {
           <li class="@i == 0 ? 'active' : ''"><a href="#@Model.Translations.Keys.ToList()[i]" data-toggle="tab">@Model.Translations.Keys.ToList()[i]</a></li>
        }
     </ul>
     <div class="tab-content" style="overflow: visible;">
         @for (int i = 0; i < Model.ExpirimentToRemove.Count; i++)
         {
             <div class="tab-pane@i == 0 ? ' active' : ''" id="@@Model.Translations.Keys.ToList()[i]">
                @Html.TextBoxFor(m => Model.ExpirimentToRemove[i].Value.Title)
                @Html.TextBoxFor(m => Model.ExpirimentToRemove[i].Value.FullDescription)
                @Html.TextBoxFor(m => Model.ExpirimentToRemove[i].Value.PreviewDescription)
             </div>
          }
      </div>

1 个答案:

答案 0 :(得分:1)

要绑定的项目列表的技巧是隐藏字段。

Here是一篇很好的文章,详细解释了它

示例:

<input type="hidden" name="products.Index" value="caliente" />
<input type="text" name="products[caliente].Name" value="Salsa" />
<input type="text" name="products[caliente].Price" value="1.23" />

唯一的另一种方法是手动操作与List<T>中给定项目的对象路径对应的元素的name属性。