[英]asp net mvc post gives empty viewmodel
因此,我有一个迭代的视图模型列表,并且视图模型对象中的属性之一是字典。 在我的customercontroller中,在details操作中,我从asp-route中获得了与id对应的所有视图模型,在视图中,我具有一个形式,其中提供了所有字典值,以便您可以根据需要进行修改。 之后,您可以提交表格。 这是我认为viewmodel列表为“ 0”的地方。 为什么是这样?
这是我的模型:
public class CustomerViewModel
{
public CustomerViewModel()
{
EmployeeAndHours = new Dictionary<string, int>();
}
public string projectName { get; set; }
public Dictionary<string, int> EmployeeAndHours { get; set; }
}
这是获取操作:
// GET: Customers/Details/5
public IActionResult Details(int? id)
{
if (id == null)
{
return NotFound();
}
var customers = _customerHelper.GetCustomerDetails(id);
if (customers == null)
{
return NotFound();
}
return View(customers);
}
这是后期操作:
[HttpPost]
public IActionResult EditCustomerDetailViewModel(List<customerViewModel> customers)
{
//TODO
return View("Details");
}
这是我的看法:
@model List<myNamespace.Models.ViewModels.CustomerViewModel> <div class="container-fluid"> <form asp-controller="Customers" asp-action="EditCustomerDetailViewModel" method="post"> @foreach (var customer in Model) { <div class="media"> <div class="media-body"> <div class="text-wrapper"> <h5 class="mt-0">@customer.projectName</h5> </div> <div class="slider-wrapper"> @foreach (var employee in customer.EmployeeAndHours) // This is the dictionary { <input name="@("EmployeeAndHours[" + employee.Key + "]")" type="range" min="0" max="1000" step="1" value="@employee.Value" data-orientation="horizontal"> <hr /> } </div> </div> </div> } <div class="form-group" style="text-align:center"> <input id="customer-detail-form-button" type="submit" value="Save changes" class="btn btn-success" /> </div> </form> </div>
您不能使用foreach
循环来生成收集项目的表单控件并获得正确的2向模型绑定。 如将HTML表发布到ADO.NET DataTable中所述for
您需要使用for
循环或将EditorTemplate
用于typeof CustomerViewModel
。
另外,应该避免绑定到Dictionary
因为不能使用强类型的HtmlHelper
方法或TagHelpers进行2向模型绑定。
为了绑定到当前模型,您的name
属性必须采用name="[#].EmployeeAndHours[Key]"
的格式,其中#
是从零开始的集合索引器。
而是将视图模型修改为
public class CustomerViewModel
{
public string ProjectName { get; set; }
public List<EmployeeHoursViewMode> EmployeeHours { get; set; }
}
public class EmployeeHoursViewModel
{
public string Employee { get; set; }
public int Hours{ get; set; }
}
然后视图变成
@model List<CustomerViewModel>
<form asp-controller="Customers" .... >
@for(int i = 0; i < Model.Count i++)
{
....
<h5>@Model[i].ProjectName</h5>
@Html.HiddenFor(m => m[i].ProjectName)
// or <input type="hidden" asp-for-"Model[i].ProjectName />
<div>
@for(int j = 0; j < Model[i].EmployeeHours.Count; j++)
{
@Html.HiddenFor(m => m[i].EmployeeHours[j].Employee)
@Html.LabelFor(m => m[i].EmployeeHours[j].Hours, Model[i].EmployeeHours[j].Employee)
// or <label asp-for="Model[i].EmployeeHours[j].Hours">@Model[i].EmployeeHours[j].Employee</label>
@Html.TextBoxFor(m => m[i].EmployeeHours[j].Hours, new { type = "range", ... })
// or <input asp-for-"Model[i].EmployeeHours[j].ProjectName type="range" ... />
}
</div>
}
<input type="submit" value="Save changes" class="btn btn-success" />
}
请注意,以上代码假定您要回发ProjectName
的值(因此为隐藏输入),并且要在每个“小时”输入旁边显示员工姓名。
您没有通过POST操作引用模型名称,请尝试以下操作:
@{int index = 0;}
@foreach (var customer in Model)
{
...
@foreach (var employee in customer.EmployeeAndHours)
{
<input name="customer[@(index)].EmployeeAndHours[@(employee.Key)]" type="range" min="0" max="1000" step="1" value="@employee.Value" data-orientation="horizontal">
<hr />
}
...
index++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.