我有一个包含多个列表的模型,我可以在我的视图中显示列表中的所有信息,但是当我将其发回时,我的所有列表都为空。我的模型如下(简化为简洁)。
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; }
}
我的帖子控制器
[HttpPost]
public virtual PartialViewResult UpdatePart(PartDetail part)
{
part.Update();
return PartList(part.PartType);
}
我的观点是(为简洁而缩短)
@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)
}
答案 0 :(得分:5)
使用 For 循环而不是foreach,在这种情况下,模型绑定器可以区分字段并恢复列表,例如此Html.TextboxFor(model =&gt; model.DateAttributes [i])