2013-03-09 170 views
0

我有一个ID,Area,Debug,Error,Info,Warning,致命属性的模型,我想用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"); 
    } 

的问题是,当我取消选中复选框一行,并提交表单对应的行成为虚假的价值,但这时如果我检查了这复选框并提交表单它不会变成true,并且在这种情况下,当我在保存操作中设置断点时,我看到的值是stil false。

感谢您的任何帮助。

+0

当您在您的操作中设置断点时,是否会获得具有预期行数的列表? – 2013-03-09 05:52:07

回答

0

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