【发布时间】:2014-06-10 14:53:22
【问题描述】:
我正在尝试在 MVC 中回发一个可编辑的列表,但是对于我的原始视图,该列表始终返回 null。我有一个视图模型列表,所有结果都显示在一个表单中,其中有一些是可编辑的。我做了一些谷歌搜索,发现这个http://forums.asp.net/t/1848323.aspx?MVC+Model+List+Httppost+always+null 几乎是同一个问题。我尝试用IList 植入它,但我仍在发回一个空列表。任何想法为什么或如何发回列表?有任何问题或是否需要更多信息/代码,请告诉我。谢谢。
使用 IEnumerable 查看原件
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "frm", id = "frm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
foreach (var item in Model)
{
<!-- Partial View Form -->
@Html.Partial("~/Views/Home/Partial/PartialEdit.cshtml", item)
<hr />
}
<div>
<p>
<input type="submit" id="submitButton" value="Save" />
</p>
</div>
}
查看当前以使用 IList
@model IList<NavLiveWebInterface.ViewModel.Proc_Item_SKUViewModel>
for (var i = 0; i < Model.Count(); i++)
{
<!-- Partial View Form -->
@Html.Partial("~/Views/Home/Partial/PartialEdit.cshtml", Model[i])
<hr />
}
局部视图
@model NavLiveWebInterface.ViewModel.Proc_Item_SKUViewModel
<div class="row">
<div class="col">
<div class="editor-label">
@Html.LabelFor(model => model.Item_SKU)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Item_SKU,new { disabled="disabled", @readonly = "readonly" })
</div>
</div>
<div class="col">
<div class="editor-label">
@Html.LabelFor(model => model.Master_SKU)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Master_SKU,new { disabled="disabled", @readonly = "readonly" })
</div>
</div>
<div class="col">
<div class="editor-label">
@Html.LabelFor(model => model.Attribute_Code)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Attribute_Code,new { disabled="disabled", @readonly = "readonly" })
</div>
</div>
<div class="col">
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name,new { disabled="disabled", @readonly = "readonly" })
</div>
</div>
<div class="col">
<div class="editor-label">
@Html.LabelFor(model => model.Sample_Quantity)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Sample_Quantity)
</div>
</div>
<div class="col">
<div class="editor-label paddinglabel">
@Html.LabelFor(model => model.Usage)
</div>
<div class="paddingdropdown">
<div class="editor-field dropdown">
@Html.DropDownListFor(model => model.Usage, Model.TrueFalse)
@Html.ValidationMessageFor(model => model.Usage)
</div>
</div>
</div>
<div class="col">
<div class="editor-label paddinglabel">
@Html.LabelFor(model => model.Filterable)
</div>
<div class="paddingdropdown">
<div class="editor-field dropdown">
@Html.DropDownListFor(model => model.Filterable, Model.TrueFalse)
@Html.ValidationMessageFor(model => model.Filterable)
</div>
</div>
</div>
<div class="col">
<div class="editor-label paddinglabel">
@Html.LabelFor(model => model.Display)
</div>
<div class="paddingdropdown">
<div class="editor-field dropdown">
@Html.DropDownListFor(model => model.Display, Model.TrueFalse)
@Html.ValidationMessageFor(model => model.Display)
</div>
</div>
</div>
<div class="col">
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Description,new { disabled="disabled", @readonly = "readonly" })
</div>
</div>
</div>
控制器 HTTP POST
[HttpPost]
public ActionResult Edit(IList<Proc_Item_SKUViewModel> vmList)
{
try
{
if (ModelState.IsValid)
{
//updates the items
_repository.updateItems(vmList);
return RedirectToAction("../Home/Index");
}
}
catch (Exception e)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(vmList);
}
【问题讨论】: