我有显示数据的强类型视图
ViewModel
public class GoldSetnUsers
{
bool Public { get; set; }
public List<GSUsers> gsUsers { get; set; }
public GoldSetnUsers()
{
UsersContext _dbm = new UsersContext();
this.gsUsers = _dbm.UserProfiles.Select(n => new GSUsers { UserName = n.UserName, isEditor = false, isReviewer = false }).ToList();
}
public class GSUsers
{
public string UserName { get; set; }
public bool isEditor { get; set; }
public bool isReviewer { get; set; }
}
}
控制器 Httpget 方法显示此视图
问题是,回发模型将所有行复选框返回为 false。但是,表外的复选框 Public 会返回正确的回发值。
控制器回发代码
[HttpPost]
public ActionResult Create(GoldSetnUsers newGS)
{
if (ModelState.IsValid)
{ // newGS gets me value
}
}
看法
@model mvc2db.Models.GoldSetnUsers
@using BootstrapSupport;
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.BeginControlGroupFor(model=>model.Public)
@Html.LabelFor(model => model.Public,new {@class="control-label"})
<div class="controls">
@Html.EditorFor(model => model.Public,new {@class="input-xlarge"})
@Html.ValidationMessageFor(model => model.Public,null,new{@class="help-inline"})
</div>
<div class="controls">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Reviewer</th>
<th>Editor</th>
</thead>
<tbody>
@foreach (var item in Model.gsUsers) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.EditorFor(modelItem => item.isEditor)
</td>
<td>
@Html.EditorFor(modelItem => item.isReviewer)
</td>
</tr>
}
</tbody>
</table></div>
@Html.EndControlGroup()
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button class="btn">Cancel</button>
</div>
</fieldset>
}