Translate

Tuesday, 21 April 2015

Cascading Dropdown List in asp.net MVC


In controller

   public ActionResult CascadingDropdown()
        {
            //Country count=new Country();
            ViewData["country"] = GetCountry();
            return View();

        }

In view

@model JQGridExample.Models.Country

@{
    ViewBag.Title = "CustomerFeedback";
}
<h2>CustomerFeedback</h2>
<link href="~/Content/css/Style.css" rel="stylesheet" />
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#Country").change(function () {
            $("#State").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetStates")',
                dataType: 'json',
                data: { id: $("#Country").val() },
                success: function (states) {
                 
                    $.each(states, function (i, state) {
                        $("#State").append('<option value="' + state.Value + '">' + state.Text + '</option>');
                    });
                },
                error: function (ex) {
                    alert('Failed to retrieve states.' + ex);
                }
            });
            return false;
        })
        $("#State").change(function () {
            $("#city").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetCity")',
                dataType: 'json',
                data: { id: $("#State").val() },
                success: function (citys) {
                    $.each(citys, function (i, city) {
                        $("#city").append('<option value="'+ city.Value + '">' + city.Text + '</option>');
                    });
                },
                error: function (ex) {
                    alert('Failed to retrieve states.' + ex);
                }
            });
             return false;
         })

    });
</script>

@using (Html.BeginForm())
{
    <div class="editor-label">
        <br />
        <div style="color: Purple;">
            @Html.Label("Select County")
        </div>
        <div class="editor-field">
          
                @Html.DropDownListFor(m=>m.CountryName, new SelectList((System.Collections.IEnumerable)ViewData["country"],"value","text"),"Select",new { style = "width:250px", @class = "dropdown1",@id="Country" })
          
        </div>
        <br />
        <div style="color: Purple;">
            @Html.Label("Select State", new { style = "width:250px" })
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(m=>m.Statename, new SelectList(string.Empty, "Value", "Text"), "Please select a State", new { style = "width:250px", @class = "dropdown1",@id="State" })
        </div>
        <br />
        <div style="color: Purple;">
            @Html.Label("Select City", new { style = "width:250px" })
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(m=>m.Cityname, new SelectList(string.Empty, "Value", "Text"), "Please select a city", new { style = "width:250px", @class = "dropdown1",@id="city" })
        </div>
        <div>
        <p class="mag">
            hello</p>
        <input type="text" class="mag">
    </div>
        <br />
        <input id="Submit1" type="submit" value="submit" />
    </div>
   
}

 In controller

  public List<Country> GetState(string id)
        {
            List<Country> lst = new List<Country>();
            Country country = null;
            int ids = Convert.ToInt32(id);
            var coun = systest.States.Where(u => u.CountryId == ids).ToList();
            for (int i = 0; i < coun.Count(); i++)
            {
                country = new Country();
                country.value = coun[i].StateId;
                country.Text = coun[i].StateName;
                lst.Add(country);
            }
            return lst;
        }

        public List<Country> GetCitys(string id)
        {
            List<Country> lst = new List<Country>();
            Country country = null;
            int idss = Convert.ToInt32(id);
            var coun = systest.Cities.Where(u => u.StateId == idss).ToList();
            for (int i = 0; i < coun.Count(); i++)
            {
                country = new Country();
                country.value = coun[i].Cityid;
                country.Text = coun[i].CityName;
                lst.Add(country);
            }
            return lst;
        }
        public JsonResult GetStates(string id)
        {

            Country count = new Country();
            List<Country> ListState = new List<Country>();
            ListState = GetState(id);
            return Json(new SelectList(ListState, "Value", "Text"));
        }

        public JsonResult GetCity(string id)
        {

            Country count = new Country();
            List<Country> ListCity = new List<Country>();
            ListCity = GetCitys(id);
            return Json(new SelectList(ListCity, "Value", "Text"));
        }

        [HttpPost]
        public ActionResult CascadingDropdown(FormCollection FC)
        {

            string country = FC["CountryName"].ToString();
            string state = FC["Statename"].ToString();
            string city = FC["Cityname"].ToString();
            ViewData["country"] = GetCountry();
            return View();
        }

 the Output like below



No comments:

Post a Comment