【问题标题】:Postback checkboxes from table rows on MVC Razor Strongly Typed ViewMVC Razor 强类型视图上表行的回发复选框
【发布时间】:2013-06-15 09:40:16
【问题描述】:

我有显示来自

的数据的强类型视图
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; }
        }

    }

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

【问题讨论】:

    标签: asp.net-mvc-4 razor checkbox html-table postback


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 2013-04-10
    • 1970-01-01
    • 2012-09-30
    • 2013-12-03
    • 2011-01-01
    • 2013-05-31
    • 1970-01-01
    相关资源
    最近更新 更多