我正在尝试填写从下拉列表中选择国家/地区的状态 ddl。我能够根据stateList
函数中的国家/地区名称标准获得过滤结果。但是当它返回视图时,它会显示所有状态列表。你能告诉我哪里出错了。
提前致谢。
//This is my controller class
public ActionResult Country()
{
Country country = new Country();
ListCountry objCountry = new ListCountry(country);
return View(country);
}
//This method calls on selection of countryanem
public ActionResult stateList()
{
Country country = new Country();
var state = Request.Form["_countryId"];
country.CountryId = _countryId;
ListCountry objCountry = new ListCountry(country);
return View(country);
}
}
//This is model class
public partial class Country
{
public SelectList countryList { get; set; }
public SelectList stateList { get; set; }
}
public partial class ListCountry
{
Entities db = new Entities();
//This constructorof Listcounty populate the country and state ddl
public ListCountry(Country country)
{
string cntId1 = country.countryId;
int id = Convert.ToInt32(cntId1);
country.countryList = new SelectList(db.Countries.ToList(), "CountryId", "CountryName");
if (id > 0 )
{
List <State> list = db.States.Where(p => p.countryId == id).ToList();
country.stateList = new SelectList(list, "StateID", "StateName");
}
else
{
country.stateList = new SelectList(db.States.ToList(), "StateID", "StateName");
}
}