Tags: , , , | Categories: development Posted by pieterg on 4/12/2010 9:18 PM | Comments (4)

First we will need to setup the JsonResult controller action.

 

/// 
/// Get Models
/// 
/// 
/// 
public JsonResult GetModels(string id)
{
	JsonResult result = new JsonResult();
	var filteredModels = from model in homeViewModel.Models
						 where model.MakeID.ToString() == id
						 select model;
	result.Data = filteredModels.ToList();
	result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
	return result;
}

 

This method now gives us the ability to use the nifty $.getJSON jquery call.
The signature for the call is as follows

jQuery.getJSON( url, [ data ], [ callback(data, textStatus) ] )

 

Given that we have setup 2 Drop Down Lists, one for Makes and the other for Models, like so.



we can include the following bit of jquery

//Hook onto the MakeID list's onchange event
$("#MakeID").change(function() {
	//build the request url
	var url = '' + "Home/GetModels";
	//fire off the request, passing it the id which is the MakeID's selected item value
	$.getJSON(url, { id: $("#MakeID").val() }, function(data) {
		//Clear the Model list
		$("#ModelID").empty();
		//Foreach Model in the list, add a model option from the data returned
		$.each(data, function(index, optionData) {
			$("#ModelID").append("" + optionData.Description + "");
		});
	});
}).change();

CascadingDropDownList.zip (365.72 kb)

Comments

camouflage cargo pants
camouflage cargo pants United States on 3/31/2010 9:47 AM thanks for this code Smile
http://www.lowest-rate-loans.com
http://www.lowest-rate-loans.com Canada on 4/2/2010 4:25 AM Have no enough cash to buy a building? Worry not, because that is real to get the loan to resolve all the problems. So get a auto loan to buy all you require.
df
df United States on 4/5/2010 1:42 PM hi..
Swimming Pool Shapes
Swimming Pool Shapes United States on 4/5/2010 2:15 PM I'll use it on my ASP.Net application; I'm very much impressed with this one, and glad that I found it in your article.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading