如何在asp.net mvc中检索控制器中的复选框列表值
我在视图页面中有一个如下所示的表单:
<form runat="server" id="dcrsubmit">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>test1</asp:ListItem>
<asp:ListItem>test2</asp:ListItem>
<asp:ListItem>test3</asp:ListItem>
<asp:ListItem>test4</asp:ListItem>
<asp:ListItem>test5</asp:ListItem>
<asp:ListItem>test6</asp:ListItem>
</asp:CheckBoxList>
....other controls
</form>
现在,当发布表单时,我尝试检索在控制器中提交的值,如下所示:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult testmethod(FormCollection formValues)
{
string s = formvalues.get("CheckBoxList1");
.
. /* other code */
.
}
当我通过选中一些复选框提交表单时,字符串值显示 null。这是检索值的方法还是我做错了什么?而且我无法使用 html 控件,因为表单上的所有其他控件都是服务器控件,我不确定是否只能将此控件设为 html 控件。我不确定如何将值绑定到它?
I am having a form in a view page that looks as below:
<form runat="server" id="dcrsubmit">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>test1</asp:ListItem>
<asp:ListItem>test2</asp:ListItem>
<asp:ListItem>test3</asp:ListItem>
<asp:ListItem>test4</asp:ListItem>
<asp:ListItem>test5</asp:ListItem>
<asp:ListItem>test6</asp:ListItem>
</asp:CheckBoxList>
....other controls
</form>
Now when the form is posted, I am trying to retrieve the values submitted in the controller as below:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult testmethod(FormCollection formValues)
{
string s = formvalues.get("CheckBoxList1");
.
. /* other code */
.
}
The string value shows null when I submit the form by checking some checkboxes. Is this the way to retrieve the values or am I doing something wrong? And I cannot use html control because all other controls on the form are server controls and I am not sure if I can only make this control a html control. And I am not sure how can I bind the values to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVCContrib 提供了一些优秀的扩展来提供许多控件,并且它们还有一个 CheckBoxList。
要开始使用 MVCContrib,阅读本文
您需要使用强类型视图并在视图中包含此导入语句:
此示例有效。这是视图上的代码。
这是控制器。
这是模型。
MVCContrib supply some excellent extensions to provide many controls and they also have a CheckBoxList.
To get started with MVCContrib, read this
You'll need to use a strongly typed view and include this import statement in the view:
This example works. Here is the code on the view.
Here is the controller.
Here is the model.
我认为您需要的是如何从用户选择的 CheckBoxList 中收集选定的值,这是我的解决方案:
1-下载 Jquery.json.js 并将其添加到您的视图作为参考:
3-如您所见,我已添加所有选定的值到一个数组,我已通过 ajax 将其传递给“Home”控制器的“保存”操作
4-在控制器中,您可以通过添加数组作为参数来接收值:
我搜索了太多,但显然这是唯一的解决方案。如果您找到更好的解决方案,请告诉我。
I think what you need is how gather selected values from CheckBoxList that user selected and here is my solution for that:
1- Download Jquery.json.js and add it to your view as refrence:
3- As you can see I've added all selected values to an Array and I've passed it to "Save" action of "Home" controller by ajax
4- in Controller you can receive the values by adding an array as argument:
I've searched too much but apparently this is the only solution. Please let me know if you find a better solution for it.