asp.net-mvc 获取控制器 mvc 4 中的复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19907004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get checkbox values in controller mvc 4
提问by Mr A
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
任何帮助或建议将不胜感激。谢谢
回答by WannaCSharp
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
然后接受一个带有参数名称的字符串数组 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();
}
回答by somesh
As per your code model.Genderis a list just for creating checkboxes.
For getting selected checkbox value, you should add a new property in you model like
根据您的代码model.Gender是仅用于创建复选框的列表。为了获得选中的复选框值,您应该在模型中添加一个新属性,例如
public string SelectedGender { get; set; }
public string SelectedGender { get; set; }
and while creating checkboxes name the checkboxes as new propertyname i.e. SelectedGender
并在创建复选框时将复选框命名为新的属性名称,即 SelectedGender
<input type="checkbox" id="SelectedGender1" name="SelectedGender" class="chkclass" value="@names.Value" />
Hope this will help you.
希望这会帮助你。

