【发布时间】:2018-03-29 01:42:48
【问题描述】:
@using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ID)
</th>
<th>
@Html.DisplayNameFor(model => model.PracticeExercise.Muscle)
</th>
<th>
@Html.DisplayNameFor(model => model.PracticeExercise.Execrcise)
</th>
<th>
@Html.DisplayNameFor(model => model.PracticeExercise.PlannedSets)
</th>
<th>
@Html.DisplayNameFor(model => model.PracticeExercise.PlannedRepeats)
</th>
<th>
@Html.DisplayNameFor(model => model.PracticeExercise.Pause)
</th>
<th colspan="5">
Weights
<th colspan="5">
Repeats
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.PracticeExercise.Muscle)
</td>
<td>
@Html.DisplayFor(modelItem => item.PracticeExercise.Execrcise)
</td>
<td>
@Html.DisplayFor(modelItem => item.PracticeExercise.PlannedSets)
</td>
<td>
@Html.DisplayFor(modelItem => item.PracticeExercise.PlannedRepeats)
</td>
<td>
@Html.DisplayFor(modelItem => item.PracticeExercise.Pause)
</td>
<td colspan="5">
@Html.EditorFor(modelItem => item.Weights1,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Weights2,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Weights3,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Weights4,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Weights5,
new { htmlAttributes = new { @class = "form-control" } })
<td colspan="5">
<div class="form-group">
@Html.EditorFor(modelItem => item.Repeats1,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Repeats2,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Repeats3,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Repeats4,
new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(modelItem => item.Repeats5,
new { htmlAttributes = new { @class = "form-control" } }
</div>
</td>
<td>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</td>
</tr>
}
</table>
}
嗨,
我使用来自数据库的输入创建了一个表。我对此页面的输入看起来像@model IEnumerable<HomePageProject.Models.Record>。正如您在代码中看到的,我将所有 Record 数据集插入到表中。
我需要帮助的是我想刷新数据库中的数据集。因此我尝试了几种控制器方法,例如[HttpPost]public ActionResult Edit(Record record) 或[HttpPost]public ActionResult Edit(IEnumerable<HomePageProject.Models.Record> lstRecords)。
在参数Record record 的情况下,有一个返回的Record 对象,但ID 为0,并且每个其他参数都包含值null。
我的计划是实现“保存整个记录列表”和“保存单个记录”的功能。你能告诉我一种方法吗?
非常感谢!
Gr_
【问题讨论】:
-
您不能使用
foreach循环为集合中的项目生成控件。参考this answer -
感谢您的链接! for 循环的提示非常好。但是现在我遇到了问题,每次我按下提交按钮时,它都会捕获 List 中的第一个元素并将其提供给 post 控制器。
-
如果您使用包含
for循环和提交按钮的单一表单,它将发回整个集合,而不是一个。请注意,您不能同时拥有这两种方式 - 您不能保存单个记录和记录列表。 (尽管您可以使用第二种形式来创建回发到不同控制器方法的新记录。
标签: asp.net-mvc-5