This article is continuation of my previous article on Populating Dropdown list in ASP.NET MVC 2 using Entity Framework 4 @ http://www.c-sharpcorner.com/UploadFile/2124ae/5628/ Here I will demonstrate Cascading dropdown list (populating a child dropdown based on selection of a parent dropdown) . We will be using ajax method in Jquery by using JSON.Over the course of this article - We will also see how to update Models using the Update Model Wizard in Entity Framework 4.0I would be referring to database, Models, Views and Controllers used in my previous article @ (http://www.c-sharpcorner.com/UploadFile/2124ae/5628/) and even the code discussed below would be an addition to the existing sample of the previous article. 1. Update ViewModelOpen IndexViewModel.cs and add code for second dropdown- Cities Drop down list which would serve as child dropdown for the State dropdown (parent dropdown).//2nd DropDownList IDpublic string ddlCityId { get; set; }
//2nd DropDownList Valuespublic List<SelectListItem> CityValue { get; set; }2. Database part
var url = '<%= Url.Content("~/") %>' + "Home/Cities_SelectedState"; var ddlsource = "#ddlStateId"; var ddltarget = "#ddlCityId"; $.getJSON(url, { Sel_StateName: $(ddlsource).val() }, function (data) { $(ddltarget).empty(); $.each(data, function (index, optionData) { $(ddltarget).append("<option value='" + optionData.Text + "'>" + optionData.Value + "</option>"); }); }); }); }); </script>
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: