【问题标题】:MVC ViewModel collections are null on postbackMVC ViewModel 集合在回发时为空
【发布时间】:2016-02-13 23:18:41
【问题描述】:

我有一个包含类引用和 2 个 IEnumerable 集合的视图模型。

public class BuildingTypeViewModel
{
    public Static_Item BuildingType { get; set; }
    public IEnumerable<Reading_Type> ReadingTypes { get; set; }
    public IEnumerable<Building_Type_Reading> BuildingReadings { get; set; }
}

我在控制器上的 Edit 操作中填充此 ViewModel

    public ActionResult Edit(int id)
    {            
        Static_Item staticItem = db.Static_Item.Find(id);

        BuildingTypeViewModel model = new BuildingTypeViewModel
        {
            BuildingType = staticItem,
            ReadingTypes=db.Reading_Type.ToList(),
            BuildingReadings = db.Building_Type_Reading.Where(bt => bt.UN_Building_Type == staticItem.UN_Building_Type).ToList()
        };

        return View(model);
    }

视图模型上的建筑类型是一个具有 ID 和描述的类,数据将是这样的:

UN_Building_Type=1, Description = "Hospital"

Reading_Type 的 IEnumerable 是这样的:

UN_Reading_Type = 1, Description = "Electric"    
UN_Reading_Type = 2, Description = "Gas"

Building_Type_Readings 的 IEnumerable 是这样的:

UN_Building_Type_Readings=1, UN_Building_Type=1, UN_Reading_Type = 1, Typical=300, Good=150
UN_Building_Type_Readings=2, UN_Building_Type=1, UN_Reading_Type = 2, Typical=800, Good=400

我将此数据加载到我的视图中:

@model SSE.Enterprise.EE_Web_Portal.Models.BuildingTypeViewModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Static_Item</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.BuildingType.UN_Building_Type)

        <div class="form-group">
            @Html.LabelFor(model => model.BuildingType.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BuildingType.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BuildingType.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">Building Readings</label>
            <div class="col-md-10">
                <table class="table">
                    <tr>
                        <th/>
                        <th>
                            @Html.LabelFor(model=>model.BuildingReadings.FirstOrDefault().Typical)
                        </th>
                        <th>
                            @Html.LabelFor(model => model.BuildingReadings.FirstOrDefault().Good)
                        </th>
                    </tr>

                    @foreach (var item in Model.ReadingTypes)
                    {
                        <tr>
                            <td>
                                @Html.Label(item.Description)
                            </td>
                            <td>
                                @Html.EditorFor(model => model.BuildingReadings.FirstOrDefault(b => b.UN_Reading_Type == item.UN_Reading_Type).Typical, new { htmlAttributes = new { @class = "form-control" } })
                            </td>
                            <td>
                                @Html.EditorFor(model => model.BuildingReadings.FirstOrDefault(b => b.UN_Reading_Type == item.UN_Reading_Type).Good, new { htmlAttributes = new { @class = "form-control" } })
                            </td>
                        </tr>
                    }
                </table>
            </div>
        </div>

        <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>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是我的编辑回发方法。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "BuildingType")] BuildingTypeViewModel model)
    {   
        if (ModelState.IsValid)
        {
            db.Entry(model).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(model);
    }

当页面回发时,2 个 IEnumerable 集合为空,因此我无法保存已输入的数据。

有什么想法吗?

【问题讨论】:

  • 你的beginform不应该有更多的细节吗?比如:使用 (Html.BeginForm("Action", "Controller", FormMethod.Post,...
  • @MarkHomer:不。所有这些都是可选的。如果不传递动作和控制器,则使用当前动作和控制器。默认是 POST,所以也不需要设置。
  • 您不能使用foreach 循环来生成表单控件 - 检查您生成的 html。 name 属性与您的 mdoel 完全没有关系。您需要使用for 循环(并且您的集合属性需要是IList&lt;T&gt; 而不是IEnumerable&lt;T&gt;。或者您需要为typeof Reading_TypeBuilding_Type_Reading 使用自定义EditorTemplates
  • @StephenMuecke 看着视图我同意这一点,虽然没有仔细看,但看起来有点乱,哈哈,调试起来就像一场噩梦,我会审查设计
  • @MarkHomer,每当您在像m=&gt; m.BuildingReadings.FirstOrDefault(b =&gt; b.UN_Reading_Type == item.UN_Reading_Type).Typical 这样可怕的视图中看到某些东西时,您可以确定它需要重新设计:)

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-5 asp.net-mvc-viewmodel


【解决方案1】:

首先,通过指定属性Bind(Include = "BuildingType"),你告诉MVC 只绑定这个单一的属性。删除该属性,MVC 将尝试绑定您的 2 个IEnumerable 集合。

接下来,检查您的@Html.EditorFor 电话。我不确定 MVC 能不能理解里面的FirstOrDefault。尽量避免在视图中使用 LINQ 选择器。

然后,正如@will 提到的,尝试将IEnumerable 更改为List

【讨论】:

  • 你是对的。要正确绑定,您需要索引集合中的项目,即@Html.EditorFor(model =&gt; model.BuildingReadings[0].Typical)
  • 我已经删除了 Bind 属性和 LINQ,它仍然是一样的
  • 尝试查看您获得的页面的 HTML 源代码。您的输入应如下所示:&lt;input name="BuildingReadings[0].Typical" ... &gt;&lt;input name="BuildingReadings[0].Good" ... &gt; 等。如果所有其他方法都失败了,您始终可以按照任何有关在 MVC 中绑定到集合的教程(如 this one),然后逐渐修改它以匹配您的视图模型看看绑定在哪一步失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多