有没有办法将复选框列表绑定到 asp.net mvc 中的模型
我在这里寻找一种快速、简单的方法来在模型中发生回发时绑定复选框列表项的列表。
显然,现在常见的做法似乎是这样做的 form.GetValues("checkboxList")[0].Contains("true");
这看起来很痛苦而且不完全安全。
有没有办法在 UpdateModel(myViewModel, form.ToValueProvider());
UpdateModel(myViewModel, form.ToValueProvider()); 期间绑定复选框列表(在视图中使用或不使用帮助器创建)甚至数据数组code> 阶段将填充模型内部的 IList
或 string[]
?
I am looking here to find a quick and easy way to bind a list of checkbox list items when the postback occurs in the model.
Apparently the common way to do it now seems to do it like this form.GetValues("checkboxList")[0].Contains("true");
It seems painfull and not exactly safe.
Is there a way to bind a list of checkbox (that are created with or without an helper in the view) or even an array of data for that matters during the UpdateModel(myViewModel, form.ToValueProvider());
phase which would populate an IList<string>
or string[]
inside of the model ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从模型开始:
然后是控制器:
视图(
~/Views/Home/Index.aspx
):最后是相应的编辑器模板:
现在,当您在 POST 操作中提交表单时,您可以将获取所选值的列表及其 id。
You could start with a model:
then a controller:
a View (
~/Views/Home/Index.aspx
):And finally a corresponding editor template:
Now when you submit the form in the POST action you will get the list of selected values along with their id.
这是快速简便的方法。只需在
checkbox
输入元素中设置value
属性,并为它们指定相同的name
。 如果我在网站中实现此功能,我将创建一个CheckBox
辅助方法,该方法采用name
、value
和isChecked
参数,但这里是只包含所需 html 的视图:在您的控制器中:
IEnumerable
参数(您可以将其设为IList< /code> 如果需要)将仅包含选中项目的值。如果没有选中任何框,它将为
null
。Here's the quick and easy way. Just set the
value
attribute inside yourcheckbox
input elements and give them all the samename
. If I was implementing this in a site, I would create aCheckBox
helper method that takesname
,value
andisChecked
parameters, but here's the View with just the html needed:In your Controller:
The
IEnumerable<string>
parameter (you could make itIList<string>
if you want) will contain only the values of the checked items. It will benull
if none of the boxes are checked.使用自定义模型绑定器和常规 HTML 效果非常好...
首先,使用 Razor 语法的 HTML 表单:(
FormMethod.Post
也可以是.Get
,但不是这样。这并不重要)然后,在正确的 MVC 意义上,有一个代表您的表单提交的模型对象:
和一个自定义模型绑定器类(我喜欢将其放在正在绑定的模型中,所以我将重复上面创建的模型以显示我将其添加到里面还允许绑定器使用私有属性设置器,这是一件好事):
.GetValueAsString()
是一个扩展方法,为了清楚起见,它是:.SplitCsv()
也是一个扩展方法,但它是一个足够常见的需求和足够混乱的代码,我将把它作为练习留给读者。最后,您处理表单提交的操作:
This works pretty well using a custom model binder and regular HTML...
First, your HTML form in Razor syntax:
(
FormMethod.Post
could also be.Get
, doesn't matter for this)Then, in proper MVC sense have a model object which represents your form submission:
And a custom model binder class (I like to put this inside the model being bound, so I'll repeat the model created above to show where I'd add it. Placing it inside also allows the binder to use private property setters, which is a Good Thing):
.GetValueAsString()
is an extension method used for clarity, here it is:.SplitCsv<T>()
is also an extension method, but it's a common enough need and messy enough code that I'll leave it as an exercise for the reader.And finally, your action to handle the form submit: