简体   繁体   English

ASP.NET MVC 5中的级联下拉列表

[英]Cascading dropdown lists in ASP.NET MVC 5

I am wondering if there's some new helper or method introduced in ASP.NET MVC 5 to implement cascading dropdown lists. 我想知道是否在ASP.NET MVC 5中引入了一些新的帮助器或方法来实现级联下拉列表。 I know a way to implement cascading dropdownlist behavior in MVC 3 and MVC 4 that is by using a JSON call 我知道一种通过使用JSON调用在MVC 3和MVC 4中实现级联下拉列表行为的方法

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

So anyone knows a better way to implement cascading dropdownlists in MVC 5? 那么任何人都知道在MVC 5中实现级联下拉列表的更好方法吗?

I know that this is an old question but somebody still may find it useful 我知道这是一个老问题,但有人仍然觉得它很有用

I was searching for the same thing but din't find anything stable and useful so I ended up implementing it by myself: 我正在寻找相同的东西,但没有找到任何稳定和有用的东西,所以我最终自己实现它:

Please take a look at Mvc.CascadeDropDown helper that I created. 请看一下我创建的Mvc.CascadeDropDown帮助器。 It works with all MVC versions starting from MVC3 and doesn't require any client side libraries(It uses plain vanilla JavaScript). 它适用于从MVC3开始的所有MVC版本,并且不需要任何客户端库(它使用普通的vanilla JavaScript)。

The usage is very simple: 用法很简单:

@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 希望它对你们中的一些人有所帮助

No, there are no new helpers or methods in MVC 5 to help. 不,MVC 5中没有新的帮助器或方法可以提供帮助。

The Ajax HTML helpers have been largely ignored in the update. 更新中基本上忽略了Ajax HTML帮助程序。 There are some things that may help with stuff related to this: 有一些事情可能有助于与此相关的东西:

  1. There is a new @Html.EnumDropDownListFor() HTML helper to populate a dropdown from an Enum. 有一个新的@Html.EnumDropDownListFor()HTML帮助器来填充Enum的下拉列表。
  2. The Attribute routing functionality of the has been improved and now works with the Web API so it is much easier to map URLs to API calls. 属性路由功能已得到改进,现在可以与Web API一起使用,因此将URL映射到API调用要容易得多。
  3. You can now pass in html attibutes in the EditorFor Html helper @Html.EditorFor(m => m.FieldName, new { htmlAttributes = new { @class = "form-control" } }) 您现在可以在EditorFor Html助手@Html.EditorFor(m => m.FieldName, new { htmlAttributes = new { @class = "form-control" } })传递html属性@Html.EditorFor(m => m.FieldName, new { htmlAttributes = new { @class = "form-control" } })

I implemented cascading dropdowns last week and used the tried and true JSON call you mentioned. 我上周实施了级联下拉菜单,并使用了您提到的尝试过的真实JSON调用。 I like to use this jQuery plugin in conjunction with the Web API v2 with the new attribute routing. 我喜欢将这个jQuery插件与Web API v2一起使用,并使用新的属性路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM