Click here to Skip to main content
15,888,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//DataAccessLayer
C#
public List<Country> GetCountry()
       {
           try
           {
               return HRMModel.Countries.ToList();
           }
           catch (Exception)
           {

               throw;
           }
       }
       public List<State> GetStates(int countryId)
       {
           return (from s in HRMModel.States
                   where s.CountryId == countryId
                   select s).ToList();

       }
 public List<EmployeeModel> GetCountry()
        {
            var countryModelList = new List<EmployeeModel>();
            var countryList = dataAccess.GetCountry();
            if (countryList != null && countryList.Count > 0)
            {
                foreach (var country in countryList)
                {
                    var countryModel = new EmployeeModel();
                    countryModel.CountryId = country.CountryId;
                    countryModel.CountryName = country.CountryName;
                    countryModelList.Add(countryModel);
                }

            }
            return countryModelList;
        }
//Form ModelLayer
        public List<EmployeeModel> GetStateOnId(int countryId)
        {
            var stateModelList = new List<EmployeeModel>();
            var stateList = dataAccess.GetStates(countryId);

            foreach (var state in stateList)
            {

                var Model = new EmployeeModel();
                Model.StateId = state.StateId;
                Model.StateName = state.StateName;
                stateModelList.Add(Model);
            }
            return stateModelList;
        }
//From Cshtml code
<pre lang="HTML"><pre lang="HTML">

Country @Html.DropDownListFor(m => m.CountryId, new SelectList(Model.Countries, "CountryId", "CountryName"), "SelectOne", new {@onchange="GetStates()", style="width:170px" })
State @Html.DropDownListFor( m=>m.StateId,new SelectList(Model.States,"StateId","StateName"))
What I have tried:

i tried to binding dropdownList with json method,Model,dataAccessLayer,and ado.net entity frame work
Posted

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