我有一个填充状态的下拉列表。应根据州的选择来填充另一个下拉列表(城市)。有什么想法吗?
这是我的城市下拉列表模型:
public List <SelectListItem> Add()
{
List<SelectListItem> items = new List<SelectListItem>();
connection();
SqlCommand cmd = new SqlCommand("proccountry_dropdown1", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pstroperationflag", "S-city");
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = sdr["city"].ToString(),
Value = sdr["city"].ToString()
});
}
}
con.Close();
return items;
}
这是我的SQL服务器代码:
ELSE IF @pstrOperationFlag = 'S-city'
BEGIN
SET @strSqlQry = 'SELECT city FROM '
SET @strSqlQry = @strSqlQry + 'country_dropdown1'
PRINT @strSqlQry
EXECUTE(@strSqlQry)
END
答案 0 :(得分:1)
您需要进行ajax调用以获取城市详细信息on-change state of state:
您的观看页面
<script type="text/javascript">
$(function () {
// get region list
$('#StateId').change(function () {
$.getJSON('/Home/GetCityList/' + $('#StateId').val(), function (data) {
var items = '<option>-Select City-</option>';
$.each(data, function (i, region) {
items += "<option value='" + region.Value + "'>" + region.Text + "</option>";
});
$('#CityId').html(items);
});
});
});
</script>
以及您的控制器操作:
public JsonResult GetCityList(int? Id)
{
var citylist = _userService.GetAllCity().Where(p => p.StateId == Id).ToList();
IEnumerable<SelectListItem> selectList = from s in citylist
select new SelectListItem
{
Value = Convert.ToString(s.Id),
Text = s.Name
};
return Json(new SelectList(selectList, "Value", "Text"), JsonRequestBehavior.AllowGet);
}
您在视图上的下拉菜单
<td>@Html.DropDownList("StateId", new SelectList(Model.StateList, "Id", "Name"), "-Select State-", new { @class = "form-control" }) </td>
<td>@Html.DropDownList("CityId", new SelectList(Model.CityList, "Id", "Name"), "-Select City-", new { @class = "form-control" })</td>
答案 1 :(得分:0)
你可以放置&#34; $(&#39; #StateId&#39;)。更改(function()...&#34; $(document).ready(function(){中的代码。 ......});它会起作用。