ASP.NET MVC 2:什么模型属性数据类型将自动绑定具有多个选择的 html 选择 (DDL)?
客户有一个模型属性,需要使用逗号分隔的所选选项列表。我们将他们的选择列表(DDL)呈现为多选下拉列表。
在客户端 HTML 选择 (DDL) 中自动绑定多项选择的属性数据类型会是什么样子?
select 会像这样发布数据:
myOptions=Volvo&myOptions=Mercedes&myOptions=Audi
我们希望自动将其绑定回某个属性:
IList<string> CarChoices {get;set;}
因此 POST 操作方法参数将是 (Carform myForm)
其中包含包含三辆选定汽车的列表的 myForm.CarChoices
?
The customer has a Model property that requires a comma separated list of selected options. We present their select list (DDL) as a multi-choice drop down.
What would the property datatype look like that would autobind multi-selections in the client side HTML select (DDL)?
The select posts data like this:
myOptions=Volvo&myOptions=Mercedes&myOptions=Audi
And we want to automagically bind it back to some property:
IList<string> CarChoices {get;set;}
So the POST action method parameter would be (Carform myForm)
which would have myForm.CarChoices
which includes a List of the three selected cars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时,亲自动手使用 HTML 会更容易。我建议这样做:
显然你的模型就是你的收藏。您还可以使用 ViewData["Whatever"] 对象来传递数据,具体取决于您的选择。
Sometimes it is just easier to get your hands dirty and work with the HTML. I suggest doing something like this:
obviously your model is your collection. You can also use the ViewData["Whatever"] object to pass data as well, your choice.
我可能会误解你想要完成的事情,但我认为 Phil Haack 的这篇文章描述了如何以干净的方式完成你想要做的事情:http://haacked.com/archive/2008/10/23/model-binding-to-a-list。 ASPX
I might be misunderstanding what you're trying to accomplish but I think this post from Phil Haack describes how to do what you're attempting to do in a clean way: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx