在ASP.NET级联下拉列表MVC 5 [英] Cascading dropdown lists in ASP.NET MVC 5
问题描述
我想知道是否有一个在ASP.NET MVC 5引入来实现级联下拉列表中的一些新的辅助或方法。我知道一种方法来实现MVC 3和MVC 4是使用JSON调用级联的DropDownList行为
I am wondering if there's some new helper or method introduced in ASP.NET MVC 5 to implement cascading dropdown lists. I know a way to implement cascading dropdownlist behavior in MVC 3 and MVC 4 that is by using a JSON call
<一个href=\"http://www.dotnet-tricks.com/Tutorial/mvc/HL53191212-Custom-Validation-for-Cascading-Dropdownlist-in-MVC-Razor.html\">http://www.dotnet-tricks.com/Tutorial/mvc/HL53191212-Custom-Validation-for-Cascading-Dropdownlist-in-MVC-Razor.html
因此,任何人都知道,以实现MVC 5级联dropdownlists更好的办法?
So anyone knows a better way to implement cascading dropdownlists in MVC 5?
推荐答案
我知道这是一个老问题,但有人仍可能会发现它有用
I know that this is an old question but somebody still may find it useful
我在寻找同样的事情,但din't找到任何稳定的和有用的,所以我结束了我自己实现它:
I was searching for the same thing but din't find anything stable and useful so I ended up implementing it by myself:
请看看我创建 Mvc.CascadeDropDown 帮手。
它的工作原理与MVC3开始的所有MVC版本,而且不需要任何客户端库(它使用普通的香草的JavaScript)。
Please take a look at Mvc.CascadeDropDown helper that I created. It works with all MVC versions starting from MVC3 and doesn't require any client side libraries(It uses plain vanilla JavaScript).
的用法很简单:
@using Mvc.CascadeDropDown
//First simple dropdown
@Html.DropDownListFor(m=>m.SelectedCountry, Model.Countries,
"Please select a Country", new {@class="form-control"})
//Dropdown list for SelectedCity property that depends on selection of SelectedCountry property
@Html.CascadingDropDownListFor(
expression: m => m.SelectedCity,
triggeredByProperty: m => m.SelectedCountry, //Parent property that trigers dropdown data loading
url: Url.Action("GetCities", "Home"), //Url of action that returns dropdown data
actionParam: "country", //Parameter name for the selected parent value that url action receives
optionLabel: "Please select a City", // Option label
disabledWhenParrentNotSelected: true, //If true, disables dropdown until parrent dropdown selected
htmlAttributes: new { @class = "form-control" }) //Html attributes
希望这将有利于一些你
Hopefully it will be helpful for some of you
这篇关于在ASP.NET级联下拉列表MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!