将复杂对象列表绑定到模型

时间:2013-03-09 05:41:45

标签: asp.net-mvc data-binding

我有一个ID,区域,调试,错误,信息,警告,致命属性的模型,我想在MVC中使用模型绑定器绑定此对象的列表。

@for (int i = 0; i < Model.Count(); i++)
    {
        <tr>
            <td>
                <input type="hidden" name="[@i].ID" value="@Model.ElementAt(i).ID"/>
            </td>
            <td>
                <input type="hidden" name="[@i].Area" value="@Model.ElementAt(i).Area" />
                @Model.ElementAt(i).Area
            </td>
            <td>
                <input name="[@i].Debug" type="checkbox" id="[@i].Debug" class="regular-checkbox" @(Model.ElementAt(i).Debug ? "checked='checked'" : "") value="@Model.ElementAt(i).Debug"/><label  for="[@i].Debug" ></label>
            </td>
            <td>
                <input name="[@i].Error" type="checkbox" id="[@i].Error" class="regular-checkbox" @(Model.ElementAt(i).Error ? "checked='checked'" : "") value="@Model.ElementAt(i).Error"/><label for="[@i].Error" ></label>
            </td>
            <td>
                <input name="[@i].Info" type="checkbox" id="[@i].Info" class="regular-checkbox" @(Model.ElementAt(i).Info ? "checked='checked'" : "") value="@Model.ElementAt(i).Info"/><label for="[@i].Info" ></label>
            </td>
            <td>
                <input name="[@i].Warnning" type="checkbox" id="[@i].Warnning" class="regular-checkbox" @(Model.ElementAt(i).Warnning ? "checked='checked'" : "") value="@Model.ElementAt(i).Warnning"/><label for="[@i].Warnning" ></label>
            </td>
            <td>
                <input name="[@i].Fatal" type="checkbox" id="[@i].Fatal" class="regular-checkbox" @(Model.ElementAt(i).Fatal ? "checked='checked'" : "") value="@Model.ElementAt(i).Fatal"/><label for="[@i].Fatal" ></label>
            </td>

        </tr>
    }

我也有一个动作:

[HttpPost]
    public ActionResult EditAll(IList<LogSetting> logsettings)
    {
        if (ModelState.IsValid)
        {
            foreach (var logsetting in logsettings)
            {
                db.LogSettings.Attach(logsetting);
                db.ObjectStateManager.ChangeObjectState(logsetting, EntityState.Modified);
            }
            db.SaveChanges();
        }

       return RedirectToAction("Index");
    }

问题是,当我取消选中一行的复选框并提交表单时,相应行的值变为false,但是如果我选中此复选框并提交表单则不会成立,在这种情况下,当我在保存操作上设置断点我看到值是stil false。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

不确定这是否与您的问题有关,但我有类似的结果。当我发布一个带有复选框并禁用的表单时,发布的值将设置为false。在发布之前删除"disabled"属性解决了问题。

相关问题