Turn the checkboxes from the rows of the table on MVC Razor Strongly typed view

I have a strongly typed view displaying 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; } } } 

Httpget controller method displays this view

enter image description here

The problem is that the post-back model returns all rows as flags. A checkbox outside the table, Public, returns the correct feedback value.

Controller Return 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> } 
+4
source share
1 answer

I assume that from the moment the flags are created using the foreach loop, all the flags will have the same identifier. Consequently, there will be ambiguity with which it is checked and which does not. You can try specifying the username as a checkbox identifier.

+4
source

Source: https://habr.com/ru/post/1486947/


All Articles