获取控制器 mvc 4 中的复选框值 [英] Get checkbox values in controller mvc 4

查看:21
本文介绍了获取控制器 mvc 4 中的复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从复选框列表中检索选中的复选框值但没有成功,下面是我尝试过的代码:

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

型号

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

控制器

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

查看

@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

推荐答案

为您的复选框指定一个名称:

assign a name to your checkbox:

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

然后接受一个带有参数名gender

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

这篇关于获取控制器 mvc 4 中的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆