Welcome Guest.

1Answers

How to use the three-level linkage of database binding in MVC

Asked by: Frances Morgan 190 views IT September 28, 2018

is the database {province Id, province Name} , {city Id City Name Province Id}, {Zone Id, District Name City Id}

1 Answers

  1. +3Votes  

    1 public ActionResult Edit(string id) 2         { 3             //Query the shipping address information 4             var data = AddressDirectoryLogic.GetById(id); 5  6             //First install the province to ViewBag 7             var provinces = RegionLogic.GetChinaProvinces(); 8             List<SelectListItem> pItems = new List<SelectListItem>(); 9             foreach (var p in provinces)//According to the list for the province name and ID 10             {11                 pItems.Add(new SelectListItem { Text = p.Name, Value = p.Id });12             }13             ViewBag.Province = pItems;14 15             //City 16 &nb Sp;           var city = RegionLogic.GetCities(data.Province);17             List<SelectListItem> item = new List<SelectListItem>();//Get City List 18             foreach (var c in city)//Get City Name and ID from List  19             {20                 item.Add(new SelectListItem { Text = c.Name, Value = c.Id });21             }22             ViewBag.City = item;23             //Show the provinces in the shipping address information and City value saved, good to display 24             ViewBag.ProvinceId = data.Province;25             ViewBag.CityId = data.City;26 27             return View(data);28         }

    js

    1 $(documen t).ready(function () { 2             //GetProvince();//Load province 3             $ (‘#T_Province’).val(‘@ViewBag.ProvinceId’); 4             $(‘#T_City’).val(‘@ViewBag.CityId’); 5             $("#T_Province").change(function () { GetCity() });//Load city 6         }); 7         function GetProvince() { 8             $("#T_Province").empty();// Empty province SELECT control 9             $.getJSON("/AddressDirectory/GetProvincelist", function (data) {10                 $.each(data, function (i, item) {11                     $("<option></ Option>").val(item["Id"]).text(item["Na Me"]).appendTo($("#T_Province"));12                 });13                 GetCity();14             });15         }16         function GetCity() {17             $("#T_City").empty();//Clear City SELECT Control 18             $.getJSON("/AddressDirectory/GetCitylist", { pid: $("#T_Province").val() },19             function (data) {20                 $.each(data, function (i, item) {21                     $("<option></option>").val(item["Value"]).text(item["Text"]).appendTo( $("#T_City"));22          &nb Sp;      });23             });24         }

    Wilson- September 28, 2018 |