【发布时间】:2015-07-14 21:04:36
【问题描述】:
我有一个具有 List 类型属性的模型。我使用此属性在我的视图中显示值。我使用 foreach 循环遍历列表中的每个项目并使用 DisplayFor 和 TextBoxFor 显示它们。那部分工作正常;我之前使用过 TextBoxFor,任何用户输入的文本都会在提交时传递给模型。但是,使用列表时,提交表单时列表为空。模型中的所有其他属性都已更新,我可以正确访问它们。是什么阻止了列表绑定,我该如何解决?
ModelInstrumentListingDetail
public class ModelInstrumentListingDetail
{
public int InstrumentTagID { get; set; }
public string InstrumentTagDescription { get; set; }
public List<ModelInstrumentListingItem> items { get; set; }
}
ModelInstrumentListingItem
public class ModelInstrumentListingItem
{
public string Field { get; set; }
public string Label { get; set; }
}
查看
@model GPC.Models.ModelInstrumentListingDetail
@using (Html.BeginForm("InstrumentListingAdd", "Home", FormMethod.Post, new { id = "InstrumentListingDetailForm" }))
{
<table>
<tr>
<td>
@Html.TextBoxFor(m => m.InstrumentTagDescription)
</td>
</tr>
@foreach (GPC.Models.ModelInstrumentListingItem item in Model.items)
{
<tr>
<td>
@Html.DisplayFor(m => item.Label)
</td>
<td>
@Html.TextBoxFor(m => item.Field)
</td>
</tr>
}
</table>
<input id="submitInstrumentListingAdd" name="submitInstrumentListingAdd" value="Add" type="submit" class="SaveButton" />
}
控制器
[HttpPost]
public ActionResult InstrumentListingAdd(ModelInstrumentListingDetail model, string submitInstrumentListingAdd)
{
...
}
提交时,发布到控制器的模型具有带有值的 InstrumentDescription,但 items 为空。我需要弄清楚如何用新值填充项目。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 model-view-controller