Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Loading 2nd drop down list on selection of 1st drop down list value in MVC
Posted

1 solution

//in view
$('#drpdwn1').bind('change', function ()
{

var id = $('#drpdwn1').val();

if (id != "" && id != null && id != undefined)
{
$.ajax({
url: '/controller/action/',
type: 'POST',
contentType: 'application/json;charset=utf-8',
data: '{"id":"' + id + '"}',
success: function (response)
{
var drpdwn2 = $('#drpdwn2');
drpdwn2.empty();
drpdwn2.append('<option value="">--Select--</option>');
$.each(response, function (index, month)
{
drpdwn2.append($('<option />',
{
value: month.intValue,
text: month.stringValue
}));
}

});


});


In controller action

C#
var result = from c in DB.tableName
                     where c.col_name == Id
                     select new IntStringPair
                     {

                         intValue = c.Id,
                         stringValue = c.name
                     };


return Json(result , JsonRequestBehavior.AllowGet);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900