4

我有显示数据的强类型视图

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

1 回答 1

4

我猜由于您使用 foreach 循环生成复选框,因此所有复选框都将具有相同的 ID。因此,对于哪个被检查,哪个没有被检查,将存在歧义。您可以尝试将用户名作为复选框的 id。

于 2013-06-19T04:59:41.053 回答