Get checkbox values in controller mvc 4
Asked Answered
M

2

5

I am trying to retrieve the checked checkbox value from a checkbox list without any success , below is the code which i have tried:

Model

 [DisplayName("Gender")]
    public IList<SelectListItem> Gender { get; set; }

Controller

 public ActionResult Index()
        {
            AssociateFormViewModel objStudentModel = new AssociateFormViewModel();

            List<SelectListItem> genderNames = new List<SelectListItem>();
            genderNames.Add(new SelectListItem { Text = "Male", Value = "1" });
            genderNames.Add(new SelectListItem { Text = "Female", Value = "2" });
            genderNames.Add(new SelectListItem { Text = "Prefer not to say", Value = "3" });

            objStudentModel.Gender = genderNames;
            return PartialView("AssociateApplicationForm", objStudentModel);

        }

[HttpPost]
        public ActionResult HandleFormSubmit(MembershipFormViewModel model)
        {
            //model not valid, do not save, but return current umbraco page
            if (ModelState.IsValid == false)
            {
                return CurrentUmbracoPage();
            }
 string test = "Gender: " + model.Gender + Environment.NewLine; //getting null here
 return RedirectToCurrentUmbracoPage();
}

View

@foreach (var names in @Model.Gender)
                {
                var checkBoxId = "chk" + names.Value;
                var tdId = "td" + names.Value;
                    <table width="100%">
                        <tr >
                            <td width="20px">
                                <input type="checkbox" id="@checkBoxId" class="chkclass" value="@names.Value" />
                            </td>
                            <td id="@tdId"  width="100px">
                                @names.Text
                            </td>
                        </tr>
                    </table>

}

Any ideas where i am getting it wrong the selected checkbox value is coming null in the post action, Also how can i limit the user to select only one check box,

Any help or suggestion will be appreciated . Thanks

Morra answered 11/11, 2013 at 12:58 Comment(0)
S
11

assign a name to your checkbox:

<input name="gender" type="checkbox" id="@checkBoxId" class="chkclass" value="@names.Value" />

Then accept a string array of with parameter name gender

[HttpPost]
        public ActionResult HandleFormSubmit(string[] gender,
             MembershipFormViewModel model)
        {
            //model not valid, do not save, but return current umbraco page
            if (ModelState.IsValid == false)
            {
                return CurrentUmbracoPage();
            }
            string test = "Gender: " + model.Gender + Environment.NewLine; //getting null here
            return RedirectToCurrentUmbracoPage();
        }
Sasha answered 11/11, 2013 at 13:48 Comment(5)
No, i meant gender. the name html attribute serve as the key to the valus posted to the controller. Your model.Gender is a ListSasha
still getting nothing :(Morra
the values is in your string[] gender parameter. not in your modelSasha
but this will give me number of selected checkbox , i want to limit user to select only oneMorra
Then use a radio button instead. Checkbox is meant for multiple selections.Sasha
P
2

As per your code model.Gender is a list just for creating checkboxes. For getting selected checkbox value, you should add a new property in you model like

public string SelectedGender { get; set; }

and while creating checkboxes name the checkboxes as new propertyname i.e. SelectedGender

<input type="checkbox" id="SelectedGender1" name="SelectedGender" class="chkclass" value="@names.Value" />

Hope this will help you.

Phototransistor answered 11/11, 2013 at 13:20 Comment(1)
Are you checking the value of model.SelectedGender in ControllerPhototransistor

© 2022 - 2024 — McMap. All rights reserved.