4

I have strongly typed view showing data from

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 method display this view

enter image description here

Problem is, post-back model returns all rows check boxes as false. The check-box outside table, Public, returns correct post-back value though.

Controller Postback code

[HttpPost]
    public ActionResult Create(GoldSetnUsers newGS)
    {
        if (ModelState.IsValid)
        {   // newGS gets me value 

          }

}

View

@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>
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user219628
  • 3,755
  • 8
  • 35
  • 37

1 Answers1

4

I guess since you are generating the checkboxes using a foreach loop all the checkboxes will have the same id. Hence there will be an ambiguity so as to which is checked and which is not. You can try giving the username as the id of the checkbox.

Kaarthik
  • 627
  • 2
  • 12
  • 32
  • Solved, thanks. Here is the code @for (int i = 0; i < Model.gsUsers.Count; i++) { @Html.EditorFor(model => model.gsUsers[i].isEditor)*@ @Html.EditorFor(model => model.gsUsers[i].isReviewer) } – user219628 Jun 19 '13 at 15:45
  • other helping article/posts on the issue http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx http://stackoverflow.com/questions/16070291/select-all-tr-of-a-table-and-send-it-to-the-controller-using-checkbox-in-mvc-4?rq=1 http://stackoverflow.com/questions/17037858/how-to-pass-ienumerable-list-to-controller-in-mvc-including-checkbox-state?rq=1 – user219628 Jun 19 '13 at 15:48