MVC data echo for three-level linkage modification

Source: Internet
Author: User

On the internet for a long time, have not found the kind of effect that they want, and finally wrote it out, although the method is a bit stupid.

This is the controller.

1  PublicActionResult Edit (stringID)2         {3             //Inquire about shipping address information4             vardata =Addressdirectorylogic.getbyid (ID);5 6             //first find the provinces and put them in the viewbag.7             varProvinces =regionlogic.getchinaprovinces ();8List<selectlistitem> Pitems =NewList<selectlistitem>();9             foreach(varPinchProvinces)//get the province name and ID based on the listTen             { OnePitems.add (NewSelectListItem {Text = p.name, Value =p.id}); A             } -Viewbag.province =Pitems; -  the             //City -             varCity =regionlogic.getcities (data. province); -list<selectlistitem> item =NewList<selectlistitem> ();//get a list of cities -             foreach(varCinchCity//get city name and ID by list +             { -Item. ADD (NewSelectListItem {Text = c.name, Value =c.id}); +             } AViewbag.city =item; at             //Save the province and city values in the shipping address information to show -Viewbag.provinceid =data. Province; -Viewbag.cityid =data. City; -  -             returnView (data); -}

Js

1 $ (document). Ready (function () {2 //getprovince ();//Loading provinces3 $ (' #T_Province '). Val (' @ViewBag. Provinceid ');4 $ (' #T_City '). Val (' @ViewBag. Cityid ');5 $ ("#T_Province"). Change (function () {getcity ()});//Load City6         });7 function Getprovince () {8 $ ("#T_Province"). empty ();//emptying the province select control9 $.getjson ("/addressdirectory/getprovincelist", function (data) {Ten $.each (data, function (I, item) { One$("<option></option>"). Val (item[" Id "]). Text (item[" Name "]). AppendTo ($ (" #T_Province ")); A                 }); - getcity (); -             }); the         } - function getcity () { - $ ("#T_City"). empty ();//Empty City Select control - $.getjson ("/addressdirectory/getcitylist", {pid: $ ("#T_Province"). Val ()}, + function (data) { - $.each (data, function (I, item) { +$("<option></option>"). Val (item[" Value "]). Text (item[" text "]). AppendTo ($ (" #T_City ")); A                 }); at             }); -}

Html

@Html. Dropdownlistfor (Model=>model. Province, (List<SelectListItem>) viewbag.province,new {@class = "combobox", id = "T_province"})                    @Html. dropdownlistfor (model = model. City, (List<SelectListItem>) viewbag.city,new {@class = "combobox", id = "T_city " })

The above part JS is referring to others. The following is part of the JS source code;

Source: http://blog.csdn.net/qq_17202783/article/details/43371421

MVC data echo for three-level linkage modification

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.