1

I want to get list of selected checkbox values. How can I do that?I'm working on an ASP.NET MVC 5 app. This app has a basic form. enter image description here

My view code

@using (Html.BeginForm())
{
    <table class="table">
        <thead>
            <tr>
                <th>Field  Name</th>
                @foreach (var field in Model.Actions)
                {
                    <th>@field.Name</th>
                }
            </tr>
        </thead>
        <tbody>

            @foreach (var permission in Model.PermissionUserField)
            {
                <tr>
                    <td> @permission.Field.Name</td>
                    @foreach (var peruserAction in permission.PermissionUserFieldActions)
                    {
                        <td>
                            @if (peruserAction.Active)
                            {
                                @Html.HiddenFor(x=> x.PermissionUserFieldForUser)

                                <input type="checkbox" checked name="field_@(permission.FieldId)_[@peruserAction.Id]" />
                            }
                            else
                            {
                                <input type="checkbox" name="field_@(permission.FieldId)_[@peruserAction.Id]" />
                            }
                        </td>
                    }

                </tr>
            }

        </tbody>
    </table>

    <br />

    <input type="submit" value="Save" />

}

The model for my form looks like the following:

 public class NewViewModel
    {
        public List<PermissionUserField> PermissionUserField{ get; set; }
        public List<PermissionUserField> PermissionUserFieldForUser { get; set; }
        public List<Action> Actions { get; set; }
        public List<Field> Field { get; set; }

    }

 public partial class Action
    {
        public int Id { get; set; }

        [StringLength(50)]
        public string Name { get; set; }

    }

    public partial class PermissionUserField
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
        public int FieldId { get; set; }
        public Field Field { get; set; }    
        public List<PermissionUserFieldAction> PermissionUserFieldActions { get; set; }
    }
    public partial class PermissionUserFieldAction
    {
        public int Id { get; set; }
        public int ActionId { get; set; }
        public Action Action { get; set; }
        public int PermissionUserFieldId { get; set; }
        public PermissionUserField PermissionUserField { get; set; }
        public bool Active { get; set; } = false;
    }

My controller code:

sab669
  • 3,984
  • 8
  • 38
  • 75
Fatikhan Gasimov
  • 903
  • 2
  • 16
  • 40
  • 1
    Um... you appear to have left your controller code out? – Timothy G. Apr 27 '17 at 12:32
  • 1
    Is there a reason you are not using Html.CheckBoxFor ? – Neil Apr 27 '17 at 12:32
  • You not giving your checkboxes a `value`. But you cannot use a `foreach` loop to generate form controls for a collection - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) nor can you bind a hidden input to a complex object. –  Apr 27 '17 at 12:33
  • Create an actionMethod within your controller, which accepts FormCollection or Your ViewModel and when you will press submit, the method will be called, you'll automatically get values of those checkboxes. – M. Adeel Khalid Apr 27 '17 at 12:33
  • Use a `for` loop and `@Html.CheckboxFor()` to bind to your `bool` property - `@Html.CheckBoxFor(m => m.PermissionUserField[i].PermissionUserFieldActions[j].IsActive)` –  Apr 27 '17 at 12:34

2 Answers2

0

The names of the checkbox should be the same to be binded in a list.

User.Anonymous
  • 1,719
  • 1
  • 28
  • 51
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/15966974) – Renats Stozkovs Apr 27 '17 at 16:25
  • The question was : `I want to get list of selected checkbox values` and an example where he's binding different name to its checkbox list. So it's not a remarks or critics, just the answer. I can write an example where the name are same if this is not clear. – User.Anonymous Apr 27 '17 at 18:27
0

you can do that with HttpPost, also you can do this with JQuery

var checkedArray=
 $('input:checkbox').each(function () {//iterate all checboxes in page
        var data={ "IsChecked":   this.checked ,"Name":this.attr('name')}
      checkedArray.push();

        });

and then send checkedArray to controller via ajax

Numan KIZILIRMAK
  • 555
  • 4
  • 20