Get checkbox values in controller mvc 4

家住魔仙堡 提交于 2019-12-03 03:51:18

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();
        }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!