使用基于另一个下拉列表的 MVC2 填充下拉列表(级联 DropDownList)
我正在制作一个处理车辆的应用程序。我需要两个 DropDownList:
- 品牌:所有车辆品牌
- 型号:属于所选的型号 Make DropDownList 的值
这在 MVC2 中是如何完成的?
我的想法:当选择第一个列表时,我是否使用 ajax 调用,然后拉回模型以绑定到模型 DDL?模型绑定如何发挥作用?
更新我发布了我最终所做的事情作为答案。它超级简单而且效果很好。
如果您愿意,也可以使用 get,但您必须指定您想要这样... return Json(citiesList, JsonRequestBehavior.AllowGet);
I am making an application that deals with vehicles. I need two DropDownLists:
- Makes: All Vehicle Makes
- Models: Models that belong to the selected
value of the Make DropDownList
How is this done in MVC2?
My Idea: Do I use an ajax call when my first list is selected and then pull back the Models to bind to the Model DDL? How would model binding come into play that way?
UPDATE I posted what I ended up doing as an answer. It is super simple and works great.
You can use a get too if you feel so inclined, but you have to specify that you want to like so... return Json(citiesList, JsonRequestBehavior.AllowGet);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我最终要做的...不需要额外的插件/1000行代码...
Html
JQuery
Action
This is what I ended up doing... Didn't need additional plugins / 1000 Lines of code...
The Html
The JQuery
The Action
这是一个很好的方法:
假设我们有两个下拉列表,国家和城市,默认情况下禁用城市下拉列表,当选择一个国家/地区时,会发生以下情况:
原始代码的来源为< a href="http://www.kingwilder.com/" rel="nofollow noreferrer">King Wilder 来自 MVC 中心。此示例是从 Golf Tracker 中的代码中提取的简化版本系列。
HTML
JavaScript
操作方法
重要说明:
1.
JsonSelectObject
有助于使将结果转换为选项标记时变得更加容易,因为它将在下面的 javascriptloadSelect
方法中使用。它基本上是一个具有两个属性值和标题的类:
2. 函数
loadSelect
是一个辅助方法,它获取最初类型为JsonSelectObject<的json对象列表/code> ,将其转换为要在调用下拉列表中注入的选项列表。这是原始代码中引用的“jQuery In Action”书中的一个很酷的技巧,它包含在您需要引用的
jquery.jqia.selects.js
文件中。这是该 js 文件中的代码:这个方法可能很复杂,,,但最后你会得到一个干净的 & 。紧凑的代码,您可以在其他地方使用。
我希望这对您有帮助,,,
更新
在 AJAX 调用中使用 POST 而不是 GET
您可以将
$.getJSON
调用替换为以下内容使用 POST 而不是 GET 进行 ajax 调用的代码。还记得通过将 [HttpGet] 注释更改为 [HttpPost] 来更改您的 Action 方法以接受 POST 请求,并在 Action 方法中返回结果时删除
JsonRequestBehavior.AllowGet
。重要说明
请注意,我们使用的是所选项目的值而不是名称。例如,如果用户选择以下选项。
那么“US”将被发送到 Action 方法,而不是“United States”
更新 2:访问控制器中的选定值
假设您的
Vehicle
viewmodel:并且您可以使用与 ViewModel 属性相同的名称来命名您的选择元素。
然后选定的值将自动绑定到您的 ViewModel,您可以直接在操作方法中访问它们。
如果页面是该 ViewModel 的强类型,这将起作用。
注意:对于第一个列表(制作列表),您可以在 ViewModel 中创建 SelectList 类型的 MakersList 并使用 HTML 帮助程序自动填充您的制作者下拉列表以及 ViewModel 中的列表。代码将如下所示:
在这种情况下,此选择的生成名称也将是“Maker”(ViewModel 中属性的名称)。
我希望这是您正在寻找的答案。
Here's a nice way to do it :
Suppose that we have two drop lists, country and city ,, the city drop down is disabled by default and when a country is selected the following happens :
Credits for the original code goes to King Wilder from MVC Central. This example is a simplified version that was extracted from his code in the Golf Tracker Series.
HTML
JavaScript
Action Method
Important Notes:
1. The
JsonSelectObject
help make things easier when converting the results to an option tag as it will be used in the javascriptloadSelect
method below.it's basically a class with the two properties value and caption :
2. The function
loadSelect
is a helper method that takes a list of json objects originally of typeJsonSelectObject
, converts it to a list of options to be injected in calling drop down list. it's a cool trick from the "jQuery In Action" book as referenced in the original code, it's included in ajquery.jqia.selects.js
file that you will need to reference. Here's the code in that js file :This method might be complex ,,, but at the end you will have a clean & compact code that you can use everywhere else.
I hope this was helpful ,,,
Update
Using POST instead of GET in the AJAX call
You can replace the
$.getJSON
call with the following code to make the ajax call using POST instead of GET.also remember to change your Action method to accept POST requests by changing the [HttpGet] annotation with [HttpPost] and remove the
JsonRequestBehavior.AllowGet
when returning the result in the Action Method.Important Note
Note that we are using the value of the selected item rather than the name. for example if the user selected the following option.
then "US" is sent to the Action method and not "United States"
Update 2: Accessing the Selected Values In The Controller
suppose that you have the following two properties in your
Vehicle
viewmodel:and you name your select elements with the same name as your ViewModel properties.
Then the selected values will be automatically Bound to your ViewModel and you can access them directly in you Action Method.
This will work if the page is Strongly Typed to that ViewModel.
Note: For the first list ( The Makes List ) you can create a MakersList of type SelectList in your ViewModel and use the HTML helper to automatically populate your Makers Drop Down with the list in your ViewModel. the code will look something like this :
In that case the generated name for this select will be also "Maker" (the name of the property in the ViewModel).
I hope this is the answer you are looking for.
最简单的方法是使用 jQuery“cascade”插件。
http://plugins.jquery.com/project/cascade (查看那里的演示页面)。
如果你想使用ajax来解析值,它也会帮助你,并且它消除了之前答案中的大量代码,因此你可以专注于你的逻辑:)
你可以在谷歌中找到很多例子,但你最终只会得到以下脚本:
The simpliest way is to use jQuery "cascade" plugin.
http://plugins.jquery.com/project/cascade (look at the demo page there).
If you want to resolve value using ajax, it also helps you and it eliminates a lot of code from the previous answer, so you can concentrate on your logic :)
You can find a lot of examples in google, but you end up with just the following script: