I have a model with multiple lists in it and I can display all the information in my lists in my view, but when I post it back all of my lists are null. My model is as follows (simplified for brevity).
public class PartDetail
{
public string DateCreated { get; set; }
[StringLength(255, MinimumLength = 3)]
public string Description { get; set; }
public Guid ID { get; set; }
public string IsActive { get; set; }
public string Manufacturer { get; set; }
public IEnumerable<SelectListItem> Manufacturers { get; set; }
[StringLength(100, MinimumLength = 3)]
public string Name { get; set; }
public string PartType { get; set; }
[Required]
[StringLength(100, MinimumLength = 3)]
public string SerialNumber { get; set; }
public List<DateAttribute> DateAttributes { get; set; }
public List<PointAttribute> PointAttributes { get; set; }
public List<TextAttribute> TextAttributes { get; set; }
public List<NumericAttribute> NumericAttributes { get; set; }
public List<LookupAttribute> LookupAttributes { get; set; }
public List<UtilizedPartAttribute> PartAttributes { get; set; }
public List<MapAttribute> MapAttributes { get; set; }
}
My Post Controller is
[HttpPost]
public virtual PartialViewResult UpdatePart(PartDetail part)
{
part.Update();
return PartList(part.PartType);
}
And my view is (shortened for brevity)
@using RIS.Models.PartModels
@model PartDetail
@using (Ajax.BeginForm("UpdatePart", "Part", null, new AjaxOptions { UpdateTargetId = "update_panel" }))
{
<h3>Part: @Model.SerialNumber</h3>
<p class="row">
<label class="span2"> Name:</label>
<strong class="span8">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</strong>
</p>
....more boilerplate code....
<h3>Attributes</h3>
foreach (var date in Model.DateAttributes)
{
if (date.IsActive)
{
<div class="row inline-inputs">
<label class="span5">@date.DisplayName:</label>
@Html.TextBoxFor(model => date.Value, new { @class = "mini" })
@Html.ValidationMessageFor(model => date.Value)
</div>
}
}
...more boilerplate code that is very similar to the above for the different lists
<p>
<input type="submit" class="btn primary" value="Save Part"/>
</p>
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.Manufacturer)
@Html.HiddenFor(model => model.DateCreated)
@Html.HiddenFor(model => model.Manufacturer)
@Html.HiddenFor(model => model.IsActive)
@Html.HiddenFor(model => model.PartType)
}
Use For loop instead of foreach, in that case model binder can distinguish fields and recover values for lists like this Html.TextboxFor(model=>model.DateAttributes[i])