This is a migrated thread and some comments may be shown as answers.

[Solved] Cascading issue when submit form

0 Answers 82 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
vinod
Top achievements
Rank 1
vinod asked on 10 Jan 2013, 04:30 PM
@model AgencyFinder.WebSite.Models.TestViewModel
@{
    ViewBag.Title = "Index";
}
<link href="~/Content/Collaspable.css" rel="stylesheet" />
<div class="collapsibleContainer">
    <h2>Search </h2>
    @using (Html.BeginForm("Index", "Test", FormMethod.Get))
 {
        @using AgencyFinder.WebSite.Models;
@model Location
@(Html.Telerik().ComboBoxFor(x => x.CountryId).Name("Loc.CountryId")
    //.AutoFill(true)
    .CascadeTo("Loc_CityId")
    .BindTo(new SelectList(Model.CountryList, "CountryId", "Name"))
    .Value(Model.CountryId.ToString())
    .ClientEvents(events =>
    {
        //events.OnChange("Loc_CountryId_OnChange");
        //events.OnClose("Loc_CountryId_OnClose");
    })
    .SelectedIndex(-1)
)
@(Html.Telerik().ComboBoxFor(x => x.CityId).Name("Loc.CityId")
    .BindTo(new SelectList(Model.CityList, "CityId", "Name"))
    //.AutoFill(true)
    .DataBinding(binding =>
    {
        binding.Ajax().Select("GetCities", "Test");
    })
    .Value(Model.CityId.ToString())
     
    .ClientEvents(events =>
    {
        events.OnDataBinding("Loc_CityId_OnDataBinding");
        //events.OnLoad("Loc_CityId_OnLoad");
 
    })
     
)
 
        <input type="submit" value="submit" />
        <input type="button" value="reset" id="reset" />
        <div id="Resulsts">
        </div>
 }
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgencyFinder.WebSite.Models;
namespace AgencyFinder.WebSite.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
        public static List<Person> Persons = new List<Person>()
        {
            new Person { Name="Vinod",Age=30,IsValid=true},
            new Person { Name="Nagi",Age=12,IsValid=true},
            new Person { Name="Ramesh",Age=33,IsValid=true},
            new Person { Name="Nishar",Age=44,IsValid=true},
            new Person { Name="Mohan",Age=55,IsValid=true},
            new Person { Name="Rohan",Age=53,IsValid=true},
            new Person { Name="Montu",Age=23,IsValid=true},
            new Person { Name="Banti",Age=5,IsValid=true},
            new Person { Name="Anuraj",Age=33,IsValid=true},
        };
        public static List<Country> Countries = new List<Country>()
        {
            new Country {CountryId=1, Name="One"},
            new Country {CountryId=2, Name="two"},
            new Country {CountryId=3, Name="three"},
            new Country {CountryId=4, Name="four"},
        };
        public static List<City> Cities = new List<City>()
        {
            new City {CityId=1, Name="One",CountryId=1},
            new City {CityId=2, Name="two",CountryId=1},
            new City {CityId=3, Name="three",CountryId=2},
            new City {CityId=4, Name="four",CountryId=1},
        };
        public ActionResult Index(TestViewModel test)
        {
            var lc =new Location();
            lc.CountryList=Countries;
            lc.CityList = Cities;
            test.Loc =lc;
            return View(test);
        }
        [HttpPost]
        public ActionResult GetCities(int Countryid)
        {
            var items=Cities.Where(x=> x.CountryId==Countryid).ToList();
            return Json(new SelectList(items, "CityId", "Name"));
        }
         
        public ActionResult Search(TestViewModel test)
        {
            return View(test);
        }
    }
}
Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace AgencyFinder.WebSite.Models
{
    public class Country
    {
        public int CountryId
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
 
    }
}
///////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace AgencyFinder.WebSite.Models
{
    public class City
    {
        public int CityId
        {
            get;
            set;
        }
        public int CountryId
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
    }
}
/////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace AgencyFinder.WebSite.Models
{
    public class Location
    {
        public List<Country> CountryList
        {
            get;
            set;
        }
        public List<City> CityList
        {
            get;
            set;
        }
        public int CountryId
        {
            get;
            set;
        }
        public int CityId
        {
            get;
            set;
        }
    }
}

If we submit form, after that If you select parent different value then It displays previous selected data value in child combo box. cascading is ok.
Tags
ComboBox
Asked by
vinod
Top achievements
Rank 1
Share this question
or