我有三个国家,州和城市的下拉菜单。但我只希望国家和国家下拉。因为国家只有一个。所以不需要国家下拉菜单。我试过但是有些课程给出了错误。请帮忙。请参阅代码供您参考。
var countries = _careerHelper.GetCountries();
countries.Insert(0, "--Select--");
ddlCountry.DataSource = countries;
ddlCountry.DataBind();
var states = _careerHelper.GetStates(ddlCountry.SelectedValue);
states.Insert(0, "--Select--");
ddlState.DataSource = states;
ddlState.DataBind();
var cities = _careerHelper.GetLocations(ddlCountry.SelectedValue, ddlState.SelectedValue);
cities.Insert(0, "--Select--");
ddlCity.DataSource = cities;
ddlCity.DataBind();
_careerhelper类代码如下: -
public List<string> GetCountries()
{
return _rblDataContext.Locations
.Select(m => m.CountryName)
.Distinct()
.ToList();
}
public List<string> GetStates(string country)
{
if (country == "")
{
return null;
}
else
{
return _rblDataContext.Locations
.Where(m => m.CountryName.ToLower() == country
.ToLower())
.Select(m => m.StateName)
.Distinct()
.ToList();
}
}
}
让我知道如何删除国家/地区下拉列表,以便我可以使用州和城市下拉菜单来管理这些内容。请帮忙
答案 0 :(得分:0)
<强>问题:强>
此处您使用国家/地区下拉列表将所选国家/地区作为参数传递。
var states = _careerHelper.GetStates(ddlCountry.SelectedValue);
解决方案1:
只需手动设置国家/地区名称,并将其作为 SpaceBison 建议传递给GetStates
。
string country = "Country Name";
var states = _careerHelper.GetStates(country);
同样的事情你也需要与Cities
:
var cities = _careerHelper.GetLocations(country, ddlState.SelectedValue);
解决方案2:
另一个解决方案是将默认值设置到下拉列表中,并保持原样。
选择国家/地区并停用下拉菜单,以便用户无法选择。
ddlCountry.Items.FindByText("India").Selected = true;
ddlCountry.Enabled = false;
完整的代码解决方案:
//populate countries
var countries = _careerHelper.GetCountries();
countries.Insert(0, "--Select--");
ddlCountry.DataSource = countries;
ddlCountry.DataBind();
//select default country
ddlCountry.Items.FindByText("India").Selected = true;
ddlCountry.Enabled = false;
//populate states
var states = _careerHelper.GetStates(ddlCountry.SelectedValue);
states.Insert(0, "--Select--");
ddlState.DataSource = states;
ddlState.DataBind();
//populate cities
var cities = _careerHelper.GetLocations(ddlCountry.SelectedValue, ddlState.SelectedValue);
cities.Insert(0, "--Select--");
ddlCity.DataSource = cities;
ddlCity.DataBind();