【发布时间】:2019-03-11 20:12:47
【问题描述】:
我有这个视图,我需要根据值列View: EstimateValue 处的所选值更新表任务。视图接受@model IEnumerable 然后迭代任务列表并将它们显示在表中。
观点
@model IEnumerable<Task>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table class="table table-striped table-hover table-bordered">
<thead>
<tr class="info">
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Value)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
@Html.HiddenFor(modelItem => item.ID)
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.EditorFor(modelItem => item.Value, new { @class = "form-control" })
@Html.ValidationMessageFor(modelItem => item.Value, "", new { @class = "text-danger" })
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Controller 有 post 方法 EstimateValue
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EstimateValue(IEnumerable<Task> TaskList)
{
if (ModelState.IsValid)
{
foreach (var task in TaskList)
{
db.Entry(task).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
//1. Rebound List to view
var TaskList = db.Tasks.ToList();
//2, return model to view
return View(TaskList);
}
运行应用程序时,TaskList 为空,我无法更新表 Task。我使用 JSON 将视图中的数据作为数组发送,但 TaskList 的值仍然为空
【问题讨论】:
标签: asp.net model-view-controller