.net core MVC Core 3.1 ForEach打开视图不更新控制器上的值
请有人给我一些建议,因为我一直在绕圈子。我有一个视图模型,其中包含我的实体列表:.net core MVC Core 3.1 ForEach打开视图不更新控制器上的值,.net-core,foreach,controller,.net Core,Foreach,Controller,请有人给我一些建议,因为我一直在绕圈子。我有一个视图模型,其中包含我的实体列表: public List<Employee> EmployeeList{ get; set; } 该视图包含: @model MyProject.Models.EmployeeViewModel @{ ViewData["Title"] = "Form"; Layout = "~/Views/Shared/_Layout.cshtml"; }
public List<Employee> EmployeeList{ get; set; }
该视图包含:
@model MyProject.Models.EmployeeViewModel
@{
ViewData["Title"] = "Form";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var item in Model.EmployeeList)
{
@Html.HiddenFor(modelItem => item.EmployeeID)
@Html.EditorFor(modelItem => item.Name)
}
@using (Html.BeginForm("MyForm", "Employees", FormMethod.Post))
{
<button type="submit" class="btn btn-info" name="SubmitAction" value="Submit">Update</button>
}
谁能给我指一下正确的方向吗
非常感谢将foreach代码移动到@using(Html.BeginForm)中 已编辑 Index.cshtml
<form method="post" asp-controller="Employee" asp-action="Index">
@foreach (var item in Model.EmployeeList)
{
<input type="hidden" asp-for="item.EmployeeID" />
<input type="text" asp-for="item.Name" />
}
<button type="submit" class="btn btn-info" name="SubmitAction" value="Submit">Update</button>
</form>
于2020年2月5日编辑
将foreach更改为for循环
<form method="post" asp-action="Index">
@{
for (int i = 0; i <= Model.Count() - 1; i++)
{
<input type="hidden" asp-for="@Model[i].Id" />
<input type="text" asp-for="@Model[i].Name" />
<input type="text" asp-for="@Model[i].EmailAddress" />
<br /><br />
}
}
<button>Save</button>
</form>
@{
对于(int i=0;感谢您的建议,遗憾的是,这并没有修复它,我仍然无法从控制器中的编辑器中看到我更改的值。因为您转储了旧的mvc代码。请使用.net core mvc代码,因为您使用的是.net core.Edited post.Can check。再次感谢您的输入,遗憾的是,这没有起作用,因为我仍然没有看到更改的值视图中的值:(哦,对不起,您尝试将foreach更改为For loop.edited post。您可以检查。
<form method="post" asp-controller="Employee" asp-action="Index">
@foreach (var item in Model.EmployeeList)
{
<input type="hidden" asp-for="item.EmployeeID" />
<input type="text" asp-for="item.Name" />
}
<button type="submit" class="btn btn-info" name="SubmitAction" value="Submit">Update</button>
</form>
[HttpPost]
public IActionResult Index(Employee emp)
{
return View();
}
<form method="post" asp-action="Index">
@{
for (int i = 0; i <= Model.Count() - 1; i++)
{
<input type="hidden" asp-for="@Model[i].Id" />
<input type="text" asp-for="@Model[i].Name" />
<input type="text" asp-for="@Model[i].EmailAddress" />
<br /><br />
}
}
<button>Save</button>
</form>