Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to All,

I have three dropdownlists.
1.Organisation
2.Department
3.Grades


I want that when I select an organisation at that time department and grade drop down list should be populated, if I select 2nd organisation depend on that organisation department and grade drop down should be changed?


Can any one give me the suggestion with demo code?

I know how to do it with json result method for cascading only one drop down list.

Thanks
Posted
Updated 21-Jul-14 1:16am
v2

1 solution

For this you can use jQuery postback event.

On index action method you can get the value of Organisation
then on select organization you can do a postback to get the value in departments
then same for Grade.

use this link same type of functionality you want.
 
Share this answer
 
Comments
DT_2 21-Jul-14 7:42am    
can you just give me the code snippet that i want exaclty? Because I am unable to catch the third drop down list at the same time of second list.
Ranjeet Patel 21-Jul-14 8:58am    
Hi Again :)


Make a json in this way and return in the action result.
var result= {
Department: [
{ name : 'name 1', value : 'value 1'},
{ name : 'name 2', value : 'value 2'}
....
],
Grade: {
...
},
};

then access it in below way

$.getJSON( 'some url', function( result) {
//Do something with data

});
You can then access the data directly with result.Department and Result.Grade.

then assign the result as we assign in dropdown result.
DT_2 22-Jul-14 13:43pm    
Sir Ranjeet,

Here is my code for controller class:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OrganisationDemo.Controllers
{
public class HomeController : Controller
{
Entity _entities = new Entity();

public ActionResult Index()
{
ViewBag.OrganisationList = new SelectList(_entities.Organisations.ToList(), "OrganisationID", "OrganisationName").FirstOrDefault();
return View();
}

public JsonResult GetDepartment(int organisaionId)
{
var department = (from dept in _entities.Departments
where dept.DepartmentID == organisaionId
select dept.DepartmentName).ToList();

return Json(new SelectList(department.ToArray(), "DepartmentID", "DepartmentName"), JsonRequestBehavior.AllowGet);
}

public JsonResult GetGrade(int departmentID)
{
var gradeList = (from grade in _entities.Grades
where grade.DepartmentID == departmentID
select grade.GradeName).ToList();

return Json(new SelectList(gradeList.ToArray(), "GardeID", "GradeName"), JsonRequestBehavior.AllowGet);
}

public ActionResult About()
{
ViewBag.Message = "Your app description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}
--------------------------------------------------------------------------------\
And here is my view code:-

@model OrganisationDemo.Entity

@{
ViewBag.Title = "Home Page";
}

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div>Select Organisation : </div>
@Html.DropDownList("organisation", new SelectList(ViewBag.OrganisationList), new { id = "drporganisation"})
<br />
<select id="txtdepartment" name="department"></select>
<br />
<select id="txtgrade" name="grade"></select>
<br />
}

@section scripts
{
<script type="text/javascript">
$(document).ready(function () {
$("#txtdepartment #txtgrade").prop("disabled", true);
$("#drporganisation").change(function () {
var options = {};
options.url = "home/getdepartment";
options.type = "POST";
option.data = JSON.stringify({ organisaionId: $("#drporganisation").val() });
options.datatype = "json";
options.contentType = "application/json";
options.success = function (department) {
$("#txtdepartment").empty();
for (var i = 0; i < department.length; i++)
$("#txtdepartment").append("<option>" + department[i] + "</options>");
}
$("#department").prop("disabled", false);
$.ajax(options);
});
});
</script>
}
--------------------------------------------------------------

What could be the wrong with my code?I am unable to retrieve the any of the drop down.

Please help me.
Ranjeet Patel 23-Jul-14 5:01am    
I want that when I select an organisation at that time department and grade drop down list should be populated, if I select 2nd organisation depend on that organisation department and grade drop down should be changed?

This is what you wrote in your question.
so the code you wrote in controller is not as you write the question.

your method should return both the department and Grade at a time.

first you clear one thing

what is your need. you want to select the organization and want to get the result accordingly in Grade and Department


or you want to select the organization and then the department will fill then selection on the Department the Grade should be coming in the dropdown. (if this is the requirement then your action method are quite correct)

be in touch..
DT_2 24-Jul-14 2:03am    
Sorry Sir, my requirement has been changed and I was not informed.Apologies.

or you want to select the organization and then the department will fill then selection on the Department the Grade should be coming in the dropdown. (if this is the requirement then your action method are quite correct)

This i want. But getting error in view section.

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