Как привязать dropdownlist в модели сущностей ADO
//DataAccessLayer
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">
Страна @Html. DropDownListFor(m => m.CountryId, new SelectList(Model.Страны, "CountryId"," CountryName"), "SelectOne", new {@onchange= " GetStates ()", style= " width:170px" })
Государство @Html. DropDownListFor( m=> m.StateId,new SelectList(Model.Состояния, "StateId", " StateName"))
Что я уже пробовал:
я попытался связать dropdownList с помощью метода json, модели, dataAccessLayer и ado.net работа с фреймом сущности