MVC3中的CheckboxList视图并获取传递给控制器的选中项
我有一个 MoreInfo 类:
public class MoreInfo
{
public string Name { get; set; }
public string selectedCheckboxItems {get; set;}
}
我想知道如何在视图上创建一个复选框列表,并在提交时将选中的项目传递给我的控制器。
我将如何创建复选框列表以及如何传递所有选中的项目并处理它们?
I have a class for MoreInfo:
public class MoreInfo
{
public string Name { get; set; }
public string selectedCheckboxItems {get; set;}
}
I want to know how to create a checkbox list on the view and pass the checked off items to my controller on submit.
How would I go about creating the checkbox list and how to pass all the checked items and process them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们稍微修改一下您的模型:
然后您可以有一个控制器:
然后您将有一个相应的视图(
~/Views/Home/Index.cshtml
),其中包含允许用户检查/的表单取消选中值:最后是编辑器模板(
~/Views/Home/EditorTemplates/ItemViewModel.cshtml
):Let's modify your model a little:
then you could have a controller:
then you would have a corresponding view (
~/Views/Home/Index.cshtml
) which would contain the form allowing the user to check/uncheck values:and finally the editor template (
~/Views/Home/EditorTemplates/ItemViewModel.cshtml
):控制器操作:
Razor 视图:
更多信息
此处
Controller Action:
Razor View:
More information
here
就这么简单:
1. 创建带有字符串 id 和布尔值的复选框类。
2. 将复选框列表放入控制器方法中并指定名称。
3. 在视图中动态创建 2 个字段,但确保符合 Razor 引擎命名系统。
要创建动态复选框列表,您需要了解剃刀引擎的工作方式,
假设你有这个代码
在视图的头部,您包含一个像这样的模型:
该模型有一个设置类,里面有一个 bool ,如下所示:
在视图中,您有:
简而言之,这将创建以下 html 代码:
到目前为止,对于 CheckBoxFor 来说效果很好,这是框架的一部分,我们如何使用数组?
所以现在我们需要做的就是了解如何在控制器方法中使用列表,
假设你有这个类:
并且在控制器中你有这个:
视图将如下所示:
不要忘记视图中的表单:
现在呈现的页面在复选框方面将如下所示:
你需要注意的是为输入复选框和输入隐藏指定的名称,我们使用它类似于剃刀引擎创建名称的方式,因此在提交后,引擎会将其呈现为数组,因此您可以在需要的地方创建任何动态复选框列表就像使用任何其他语言一样(例如 php 等...)。
就这么简单:
就这么简单:
1. 创建带有字符串 id 和布尔值的复选框类。
2. 将复选框列表放入控制器方法中并指定名称。
3. 在视图中动态创建 2 个字段,但确保符合 Razor 引擎命名系统。
我希望它有帮助。
its that easy:
1. create the checkbox class with string id and bool value.
2. put list of checkbox in the controller method with some name.
3. create 2 fields dynamically in your view but make sure you conform to the razor engine naming system.
to create a dynamic checkbox list you need to understand the way the razor engine works,
say you have this code
in the head of the view you include a model like so:
that model has a setting class that has a bool inside like so:
and in the view you have:
in short this creates the following html code:
so far so good for the CheckBoxFor, that is a part of the framwork, how do we work with arrays?
so now all we need to do is to understand how to work with list in a controller method,
say you have this class:
and in the controller you have this:
and the view will look like so:
lets not forget the form in the view:
now the rendered page will look like so in the checkbox aspect:
what you need to notice is the names given to the input-checkbox and the input-hidden we used it similar to the way razor engine creates name and so after submit the engine will render this as an array and so you can create any dynamic checkbox list where ever you want same as you would in any other language (say php and so...) .
its that easy:
its that easy:
1. create the checkbox class with string id and bool value.
2. put list of checkbox in the controller method with some name.
3. create 2 fields dynamically in your view but make sure you conform to the razor engine naming system.
i hoped it helped.