Multi-level linkage extension implementation in MVC (non-recursive form) and mvc Recursion

Source: Internet
Author: User

Multi-level linkage extension implementation in MVC (non-recursive form) and mvc Recursion

The MVC front-end interface is called as follows:

 @Html.AreaDropDownList("areaCode", areaCode, 3, string.Empty)

Parameter description:

The name of the first parameter control;
The region code selected by the second parameter;
Third parameter region level;
The fourth parameter is the root-level region;

The region database table is designed as follows:

Extension of the drop-down list:

1 # region drop-down list 2 3 /// <summary> 4 // linkage drop-down list 5 /// </summary> 6 /// <param name = "htmlHelper"> extended HtmlHelper instance </param> 7 // <param name = "expression"> get data set </param> 8 /// <param name = "level"> display level </param> 9 // <param name = "defaultValue"> default value of the TProperty type (for example, the default value of the string type is "") </param> 10 /// <param name = "rootItems"> get the root-Level list data </param> 11 /// <param name = "getParentID"> get the list item </param> 12 // <param name = "g EtChildItems "> method for getting data sets in sublists </param> 13 // <param name =" getChildSelectDataUrl "> remote address for obtaining data in sublists </param> 14/ // <returns> html code </returns> 15 public static MvcHtmlString LinkageDropDownListFor <TModel, TProperty> (this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> expression, TProperty defaultValue, int level, Dictionary <TProperty, string> rootItems, Func <TProperty, TProperty> getParentID, Func <TProperty, Dictionary <TProperty, string> getChildItems, string getChildSelectDataUrl, string optionLabel = "select") 16 {17 ModelMetadata metadata = ModelMetadata. fromLambdaExpression (expression, htmlHelper. viewData); 18 19 return LinkageDropDownList (htmlHelper, ExpressionHelper. getExpressionText (expression), (TProperty) metadata. model, defaultValue, level, rootItems, getParentID, getChildItems, get ChildSelectDataUrl, optionLabel ); 20} 21 22 23 // <summary> 24 // linkage drop-down list 25 /// </summary> 26 /// <param name = "htmlHelper"> expanded htmlHelper instance </param> 27 // <param name = "level"> display level </param> 28 /// <param name = "defaultValue"> TProperty type (For example, the default value of string is "") </param> 29 // <param name = "name"> drop-down list form item name </param> 30 /// <param name = "selectedValue"> currently selected value </param> 31 // <param name = "rootItems"> get the root-Level list data </param> 32 /// <Param name = "getParentId"> method for obtaining the ParentID of the list item </param> 33 // <param name = "getChildItems"> method for obtaining the data set in the sublist </param> 34 // <param name = "getChildSelectDataUrl"> remote address for obtaining child list data </param> 35 // <returns> html code </returns> 36 public static MvcHtmlString LinkageDropDownList <TProperty> (this HtmlHelper htmlHelper, string name, TProperty selectedValue, TProperty defaultValue, int level, Dictionary <TProperty, string> rootIte MS, Func <TProperty, TProperty> getParentId, Func <TProperty, Dictionary <TProperty, string> getChildItems, string getChildSelectDataUrl, string optionLabel = "select ") 37 {38 string fullName = htmlHelper. viewContext. viewData. templateInfo. getFullHtmlFieldName (name); 39 // select data init40 Stack <Dictionary <TProperty, string> stack = new Stack <Dictionary <TProperty, string> (); 41 42 // If a selected value exists, search for List 43 IList <TProperty> selectedValues = new List <TProperty> (); 44 if (selectedValue! = Null &&! SelectedValue. Equals (defaultValue) 45 {46 TProperty itemId = selectedValue; 47 TProperty parentItemId = getParentId (itemId); 48 while (! ItemId. Equals (defaultValue )&&! ParentItemId. equals (defaultValue) 49 {50 stack. push (getChildItems (parentItemId); 51 selectedValues. add (itemId); 52 itemId = parentItemId; 53 parentItemId = getParentId (itemId); 54} 55 if (rootItems. count ()> 0) 56 {57 TProperty rootId = getParentId (rootItems. first (). key); 58 if (! ItemId. equals (rootId) 59 {60 stack. push (rootItems); 61 selectedValues. add (itemId); 62} 63} 64} 65 else66 {67 TProperty rootItemID = rootItems. select (n => n. key ). firstOrDefault (); 68 stack. push (rootItems); 69} 70 71 // generate TAG 72 TagBuilder containerBuilder = new TagBuilder ("span"); 73 containerBuilder. mergeAttribute ("plugin", "linkageDropDownList"); 74 var data = new Dictionary <string, object> (); 75 data. tryAdd ("GetChildSelectDataUrl", getChildSelectDataUrl); 76 data. tryAdd ("ControlName", name); 77 data. tryAdd ("Level", level); 78 data. tryAdd ("OptionLabel", optionLabel); 79 data. tryAdd ("DefaultValue", defaultValue. toString (); 80 containerBuilder. mergeAttribute ("data", Json. encode (data); 81 int currentIndex = 0; 82 while (stack. count> 0) 83 {84 Dictionary <TProperty, string> dictionary = stack. pop (); 85 IEnumerable <SelectListItem> selectList = dictionary. select (n => new SelectListItem () {Selected = selectedValues. contains (n. key), Text = n. value, Value = n. key. toString ()}); 86 containerBuilder. innerHtml + = "\ r \ n" + htmlHelper. dropDownList (string. format ("{0 }_{ 1}", name, currentIndex), selectList, 87 optionLabel, new {@ class = "tn-dropdownlist"}); 88 currentIndex ++; 89} 90 containerBuilder. innerHtml + = "\ r \ n" + htmlHelper. hidden (name); 91 return MvcHtmlString. create (containerBuilder. toString (); 92} 93 94 # endregionView Code

 

External call implementation:

1 /// <summary> 2 // region drop-down list 3 /// </summary> 4 /// <param name = "htmlHelper"> extended htmlHelper instance </param> 5 // <param name = "expression"> select the lamda expression of the category attribute in the object </param> 6 /// <param name = "areaLevel"> Region level (site region configuration is used by default) </param> 7 // <param name = "rootAreaCode"> root-level region (default site region configuration) </param> 8 public static MvcHtmlString AreaDropDownListFor <TModel> (this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, string> exp Ression, int? AreaLevel = null, string rootAreaCode = null) 11 {12 string getChildAreasUrl = "/Channel/GetChildAreas"; // obtain the remote address of the sub-region, (AreaCode, name) JSON data 13 if (areaLevel = null) 15 {16 areaLevel = 0; // The region setting level can be obtained as a configuration file 17} 18 return htmlHelper. linkageDropDownListFor <TModel, string> (expression, string. empty, areaLevel. value: obtains the (AreaCode, name) Key-Value pairs of the root region, obtains the parent region of the currently selected region, and obtains the current Child region, getChildAreasUrl ); 21} 22 23 // <summary> 24 // area drop-down list 25 /// </summary> 26 // <param name = "htmlHelper"> extended htmlHelper instance </param> 27 // /<param name = "name"> Control name attribute </param> 28 // <param name = "name"> selected region code </param> 29 /// <param name = "areaLevel"> region level (default site configuration) </param> 30 // <param name = "rootAreaCode"> root-level region (site region configuration by default) </param> 31 public static MvcHtmlString AreaDropDownList (this HtmlHelper htmlHelper, string name, string value, int? AreaLevel = null, string rootAreaCode = null) 32 {33 string getChildAreasUrl = "/Channel/GetChildAreas"; // obtain the remote address of the sub-region, (AreaCode, name) JSON data 34 if (areaLevel = null) 36 {37 areaLevel = 0; // The region setting level can be obtained as a configuration file 38} 39 return htmlHelper. linkageDropDownList <string> (name, value, string. empty, areaLevel. value: obtains the (AreaCode, name) Key-Value pairs of the root-level region, obtains the parent region of the currently selected region, and obtains the current Child region of the selected region, getChildAreasUrl); 42}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.