级联下拉列表MVC? [英] Cascading Drop Down List MVC?
本文介绍了级联下拉列表MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我无法根据CountryID,StateID将值获取到City下拉列表中,这是我的HomeController.cs类代码:
Hello All,
I am having trouble to get values according to CountryID, StateID into the City drop down list, here is my HomeController.cs class code :
public class HomeController : Controller
{
MVCDDEntities _entities = new MVCDDEntities();
public ActionResult Index()
{
ViewBag.Countries = _entities.Countries.ToList();
ViewBag.States = _entities.States.ToList();
ViewBag.Cities = _entities.Cities.ToList();
return View();
}
//Get all the states based on CountryID
public IList<state> GetStates(int countryID)
{
return _entities.States.Where(c => c.CountryID == countryID).ToList();
}
//Get all the cities based on StateID
public IList<city> GetCitis(int stateID)
{
return _entities.Cities.Where(c => c.StateID == stateID).ToList();
}
//filter state based on countryID
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult LoadStatesByCountryID(string countryID)
{
var stateList = this.GetStates(Convert.ToInt32(countryID));
var stateData = stateList.Select(c => new SelectListItem() {
Text = c.StateName,
Value = c.StateID.ToString(),
});
return Json(stateData,JsonRequestBehavior.AllowGet);
}
//filter city based on stateID
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult LoadCitiesByStateID(string stateID)
{
var cityList = this.GetCitis(Convert.ToInt32(stateID));
var cityData = cityList.Select(c => new SelectListItem()
{
Text = c.CityName,
Value = c.CityID.ToString(),
});
return Json(cityData, JsonRequestBehavior.AllowGet);
}
}
这是我的查看代码:
And here is my view code :
@model MVCCascading.MVCDDEntities
@{
ViewBag.Title = "Index";
}
<p>
@Html.DropDownListFor(Model => Model.Countries, new SelectList(ViewBag.Countries as System.Collections.IEnumerable, "CountryID", "CountryName"),
"Select a Country", new { id = "ddlCountry" })
</p>
<p>
@Html.DropDownListFor(Model => Model.States, new SelectList(Enumerable.Empty<SelectListItem>(), "StateID", "StateName"),
"Select a State", new { id = "ddlStates" })
</p>
<p>
@Html.DropDownListFor(Model => Model.Cities, new SelectList(Enumerable.Empty<SelectListItem>(), "CityID", "CityID"),
"Select a City", new { id = "ddlCities" })
</p>
@section scripts
{
<script type="text/javascript">
$(document).ready(function () {
$("#ddlCountry").change(function () {
var countryID = $(this).val();
$.getJSON("../Home/LoadStatesByCountryID", { countryid: countryID },
function (stateData) {
var select = $("#ddlStates");
//alert(select);
select.empty();
select.append('<option/>', {
value: 0,
text: "Select a State"
});
$.each(stateData, function (index, itemData) {
//alert(index);
alert(stateData);
alert(itemData);
select.append('<option/>', {
value: item.Value,
text: itemData.Text
});
});
});
});
});
</script>
}
我在状态列表中得到0个值?
谁能帮助我摆脱困境?
谢谢
I am getting 0 values in state list?
Can anyone help me to get out of this?
Thanks
推荐答案
(document).ready(function(){
(document).ready(function () {
( " ).change(function(){ var countryID =
("#ddlCountry").change(function () { var countryID =
( this ).val();
这篇关于级联下拉列表MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文