asp net mvc post给出了空的viewmodel

时间:2018-03-16 22:17:22

标签: c# asp.net-mvc model-binding

所以我有一个遍历的viewmodel列表,viewmodel对象中的一个属性是一个字典。在我的customercontroller中,在详细信息操作中,我获得了与asp-route中的id对应的所有视图模型,在视图中我有一个表单,其中我提供了所有字典值,以便您可以根据需要修改它们。之后您可以提交表格。这是我认为视图模型列表为“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>

2 个答案:

答案 0 :(得分:0)

您不能使用foreach循环为集合项生成表单控件并获得正确的双向模型绑定。您需要使用for循环或EditorTemplate类型CustomerViewModel,如Post an HTML Table to ADO.NET DataTable中所述。

此外,您应该避免绑定Dictionary,因为您无法使用强类型HtmlHelper方法或TagHelpers来提供双向模型绑定。

为了绑定到您当前的模型,您的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的值(因此隐藏的输入),并且您希望在每个小时&#39;时间点附近显示员工姓名。输入

答案 1 :(得分:-1)

您没有在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++;
}