ProgrammerGuide.Net | Step by Step Programmer Guide

Cascading Dropdown using Knockoutjs and ASP.NET MVC | ASP.NET MVC Tutorial | Knockoutjs Tutorial

This guide will walk you through building cascading drop down using knockoutjs and ASP.NET MVC. We leverage knockoutjs to create UI which update itself based on user interaction. Sample can be downloaded from here.

Magic of knockoutjs

We have created a ASP.NET MVC view and model with MVVM pattern used by knockoutjs. Whenever the data source refresh at that time we only need to update underlying viewModel and the rest will be taken care by knockoutjs including updating UI based on refreshed data.
Code snippet for Home controller & State model class:
public class HomeController : Controller
    public ActionResult Index()
        ViewBag.Country = new SelectList(Country.GetCountryList(), "Code", "Name");
        return View();
    public ActionResult GetStates(string id = "")
        var stateList = State.GetStates()
            .Where(s => s.CountryCode.ToLower() == id.ToLower());
        return this.Json(stateList, JsonRequestBehavior.AllowGet);
// State model class
// Property of State class is used to 
// bind view with knockoutjs viewmodel
public class State
    public string CountryCode { get; set; }
    public int StateId { get; set; }
    public string StateName { get; set; }

Code snippet for index.cshtml:
<b>Select Country :</b> @Html.DropDownList("ddlCountry", ViewBag.Country as SelectList, "Select...", new { onchange = "FetchStates();" })
<p data-bind="visible: states().length > 0">
<b>Select State :</b> <select data-bind="options: states, optionsText: 'StateName', optionsValue: 'StateId', optionsCaption: 'Choose...'"></select>

Code snippet for ViewModel defined in JavaScript:
function CascadingDDLViewModel() {
    this.states = ko.observableArray([]);
var objVM = new CascadingDDLViewModel();

We are creating a view model with states property of type ko.observableArray. Observable type in knockoutjs will notify automatically whenever underlying object is updated.
In index.cshtml snippet, we are using this states property with data-bind attribute that binds DOM element behavior with knockoutjs ViewModel. So DOM elements will be updated automatically whenever ViewModel changes. 
Following JavaScript function will be called whenever user selects a country. With the help of jQuery.getJSON method, we fetch list of states and assigning the states to the states property of the viewmodel. As the viewmodel changes. knockoutjs will take care of loading a dropdown for states.
function FetchStates() {
    var countryCode = $("#ddlCountry").val();
    $.getJSON("/Home/GetStates/" + countryCode, null, function (data) {

Sample code can be downloaded here.
Please click here for related products on Amazon!


Add comment

Want to Check Celebrities News?