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>
Cascading DropDown List in ASP.NET MVC 2 using JQuery
Populating Dropdown list in ASP.NET MVC 2 using Entity Framework 4
Kumar; Nice work, and easy to understand and implement. Could you extend it to 4 cascading dropdown lists with MVC2(or MVC3 preferrably) using JQuery. That is: State City Street Name House Number. It will be a wonderful program. I look forward to your reaction to this. ChrisWan condatasystems@live.com
Kumar; Nice work, and easy to understand and implement. Could you extend it to 4 cascading dropdown lists with MVC2(or MVC3 preferrably) using JQuery. That is: State City Street Name House Number. It will be a wonderful program. I look forward to your reaction to this. Chris Wan
Hi Kumar, Thank you in advance for your great work. I am trying ti implement the cascading ddl on my project but I´m facing a problem. Lets say i want to store the StateID(GUID) instead of the State name(which is a string). When i try to select the SelectListItems select new SelectListItem { Text = State .Name, Value = State .Id.ToString(), } I get the error " not supported exception was unhandled by user code - LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression." Is there a solution to figure this problem?
$.getJSON(url, { Sel_StateName: $(ddlsource).val() }, function (data) { $(ddltarget).empty(); $.each(data, function (index, optionData) { $(ddltarget).append("<option value='" + optionData.Text + "'>" + optionData.Value + "</option>"); }); In this part in the jquery function you have conceptually error, you put <option value='" + optionData.Text + "'> instead <option value='" + optionData.Value + "'> and the next part is the same. In your program works, because both data are the same. Despite this little error your post help me a lot. Thanks for share it with the world!.
thank you so much, I have send the email. brgds and thank you.