ASP.NET MVC4 IEnumerable空帖子

时间:2015-08-04 10:59:01

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

我已经在这个问题上阅读了几个答案,但尽管如此,我似乎已经开发了代码失明。

我有以下视图模型:

public class IndividualProductVm
{
    public virtual Products Products { get; set; }
    public ProductSummary ProductSummary { get; set; }
    public virtual IEnumerable<ProductSimpleResponse> ProductSimpleResponse { get; set; }
}

然后将其传递到视图中,然后传递到局部视图:

@model Websites.ViewModels.IndividualProductVm @{ ViewBag.Title = "Edit"; }

<h2>Edit</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "form", id = "mainForm" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.Products.Id) @Html.HiddenFor(model
=> model.ProductSummary.SupplierId) Html.RenderPartial("_IndividualProduct", Model);


<div class="form-group">
  <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Save" class="btn btn-default" />
  </div>
</div>

}

<div>
  @Html.ActionLink("Back to List", "Index", new { id = Model.ProductSummary.SupplierId }, new { @class = "btn btn-default" })
</div>

@section Scripts { @Scripts.Render("~/bundles/jqueryval") }

@model Websites.ViewModels.IndividualProductVm

<div>
    @Html.LabelFor(model => model.Products.ProductCode, htmlAttributes: new { @class = "control-label col-md-2" })
    <div>
        @Html.DisplayFor(model => model.Products.ProductCode, new { htmlAttributes = new { @class = "form-control" } })
    </div>
</div>
<div style="clear:both;"></div>
<div>
    @Html.LabelFor(model => model.Products.ProductDescription, htmlAttributes: new { @class = "control-label col-md-2" })
    <div>
        @Html.DisplayFor(model => model.Products.ProductDescription, new { htmlAttributes = new { @class = "form-control" } })
    </div>
</div>

<table class="table">
    <tr>
        <th>
            Present
        </th>
    </tr>

    @foreach (var item in Model.ProductSimpleResponse)
    {
        <tr>

            @Html.HiddenFor(modelItem => item.Id)
            @Html.HiddenFor(modelItem => item.SupplierId)
            @Html.HiddenFor(modelItem => item.ProductCode)

            <td>
                @Html.EditorFor(modelItem => item.Present)
            </td>
        </tr>
    }

</table>

但是,当我进入编辑帖子时,我的viewmodel对于IEnumerable<ProductSimpleResponse>为空,但对于其他两个类则没有问题。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(IndividualProductVm model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("Index", new { id = model.ProductSummary.SupplierId });
        }

        return View(model.Products);
    }

如果有人能解释我做错了什么,我将非常感激。

2 个答案:

答案 0 :(得分:0)

你的属性名是ProductSimpleResponse,虽然类型是ProductSvhcSimpleResponse,所以要迭代它你应该有。

@foreach (var item in Model.ProductSimpleResponse)

不是

@foreach (var item in Model.ProductSvhcSimpleResponse)

答案 1 :(得分:-1)

使用List,因为

IEnumerable仅适用于迭代通过集合,你不能修改(添加或删除)数据IEnumerable将所有数据从服务器带到客户端然后过滤它们,假设你有很多记录,所以IEnumerable会给你的内存带来开销。

public class IndividualProductVm
{
    public virtual Products Products { get; set; }
    public ProductSummary ProductSummary { get; set; }
    public virtual List<ProductSvhcSimpleResponse> ProductSimpleResponse { get; set; }
}

More help click here