我在 Razor 中有以下代码:
@for (int i = 0; i < Model.Count; i++)
{
using (Html.BeginForm("Bonnen", "Bon"))
{
<tr>
<td> @Html.TextBoxFor(model => Model[i].Id)</td>
<td>@Html.DisplayFor(model => Model[i].Date)</td>
<td>@Html.DisplayFor(model => Model[i].Description)</td>
<td><button type="submit" value="Details" class="btn btn-info">Details</button></td>
</tr>
}
}
当我将数据发布到 Controller 时,它变为空。 我已经看到一个问题,它说你不能使用 foreach 循环 here . 在网站本身上,它确实显示了所有数据,但不会将其提供给 Controller 。
Controller :
[HttpPost]
public ActionResult Bonnen(Bon bon)
{
return RedirectToAction("Details", "Bon", bon);
}
型号:
public int Id { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
最佳答案
您的代码需要在表单内,按钮类型“提交”将不起作用。您需要使用输入类型“提交”。见下面的代码:
@using (Html.BeginForm("Bonnen", "Bon"))
{
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td> @Html.TextBoxFor(model => Model[i].Id)</td>
<td>@Html.DisplayFor(model => Model[i].Date)</td>
<td>@Html.DisplayFor(model => Model[i].Description)</td>
<td><input type="submit" value="Details" class="btn btn-info"/></td>
</tr>
}
}
关于c# - ASP.NET MVC 4 - for循环中的 Razor 空帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47353192/