回发时MVC ViewModel集合为null

时间:2015-11-12 15:11:51

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

我有一个包含类引用和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; }
}

我在控制器的“编辑”操作中填充此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集合为空,因此我无法保存已输入的数据。

有什么想法吗?

TA

1 个答案:

答案 0 :(得分:2)

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

接下来,检查您的@Html.EditorFor来电。我不确定MVC能否理解FirstOrDefault内部。尝试避免视图中的LINQ选择器。

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