Subby
Subby

Reputation: 5480

ASP.NET MVC 3 Binding Unsuccessful

I am having trouble making my ASP.NET MVC 3 View bind data back to a Model.

The typical objects such as string ContactName and string Title are being sent back successfully.

My public List<KeyValuePair<string, ListingTranslation>> ExpirimentToRemove object however isn't being sent back for some reason.

The following is contains code of the specific objects that aren't being binded successfully.

<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>

Upvotes: 0

Views: 57

Answers (1)

Gabe
Gabe

Reputation: 50493

The trick for a list of items to be bound back is to have hidden fields.

Here is a great article explaining it in detail

Example:

<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" />

The only other way is to manually manipulate the name attribute for the element that corresponds with the object path for the given item in List<T>.

Upvotes: 1

Related Questions