In many situations you may need a list of related data to display on the user interface. It is not easy (like in traditional ASP.Net applications) to display a cascading dropdown box in an MVC application. The reason is that MVC is not using any web form controls and for that reason it is not an easy task to invoke the server side to generate data.
jQuery
jQuery is unobtrusive JavaScript and it has great suuport of cross browser compatibility. It is also light in weight compared to other JavaScript frameworks.
JSON
JavaScript Object Notation (JSON) is a format for storing and exchanging text information and is smaller than XML, faster and easier to parse and is language independent.
jQuery
- $(function () {
- $('#MajorCategoryDivID').hide();
- $('#MajorSubCategoryDivID').hide();
- $('#SubmitID').hide();
- $('#MainCategoryID').change(function (request, response) {
-
- var URL = $('#Form1').data('majorCategoryAction');
- $.getJSON('/Json/MajorCategory/' + $('#MainCategoryID').val(), function (data)
- {
- var items = '<option>Select a Major Category</option>';
- $.each(data, function (i, majorCategoryList) {
- items += "<option value='" + majorCategoryList.Value + "'>" + majorCategoryList.Text + "</option>";
- });
- $('#major_CategoryID').html(items);
- $('#MajorCategoryDivID').show();
- });
- });
-
- $('#major_CategoryID').change(function (request, response) {
-
- var URL = $('#Form1').data('majorSubCategoryAction');
- $.getJSON('/Json/SubCategory/' + $('#major_CategoryID').val(), function (data) {
- var items = '<option>Select a Sub Category</option>';
- $.each(data, function (i, subCategoryList) {
- items += "<option value='" + subCategoryList.Value + "'>" + subCategoryList.Text + "</option>";
- });
- $('#major_SubCategoryID').html(items);
- $('#MajorSubCategoryDivID').show();
- });
- });
-
- $('select#major_CategoryID').change(function () { $('#SubmitID').show(); });
- });
JSON
- public class JsonController : Controller
- {
-
-
- private SubMainCategoryRepository subCategoryRepository;
- private MajorCategoryRepository majorCategoryRepository;
- public JsonResult MajorCategory(int ID)
- {
- majorCategoryRepository = new MajorCategoryRepository(new MainContext());
-
-
-
- var majorCategoryList = from m in majorCategoryRepository.GetMajorCategory(ID)
- select m;
- return Json(new SelectList(majorCategoryList.ToArray(), "major_CategoryID", "major_categoryName")
- , JsonRequestBehavior.AllowGet);
- }
- public JsonResult SubCategory(int ID)
- {
- subCategoryRepository = new SubMainCategoryRepository(new MainContext());
- var subCategoryList = from m in subCategoryRepository.GetCategory()
- where m.major_CategoryID == ID
- select m;
- return Json(new SelectList(subCategoryList.ToArray(), "major_SubCategoryID", "major_SubCategoryName")
- , JsonRequestBehavior.AllowGet);
- }
Home Controller
- private MainCategoryRepository mainCategoryRepository ;
- public HomeController()
- {
-
-
- mainCategoryRepository = new MainCategoryRepository();
- }
- public ActionResult Index()
- {
- CategoryFormViewModel mymodel = new CategoryFormViewModel();
- var minCategoryList = from m in mainCategoryRepository.GetCategory()
- select m;
- mymodel.CateogryList = new SelectList(minCategoryList, "main_CategoryID", "Main_categoryName");
- return View(mymodel);
- }