C# MVC控制器查询
下面是我的cascasde下拉列表查询。国家/地区列表正在加载,但在我的下拉列表中没有国家/地区正在加载。如果有人能帮我纠正这个问题,请C# MVC控制器查询,c#,asp.net-mvc,C#,Asp.net Mvc,下面是我的cascasde下拉列表查询。国家/地区列表正在加载,但在我的下拉列表中没有国家/地区正在加载。如果有人能帮我纠正这个问题,请 public ActionResult CountryList() { var countries = db.Countries.OrderBy(x=>x.CountryName).ToList(); // IQueryable countries = Country.Ge
public ActionResult CountryList()
{
var countries = db.Countries.OrderBy(x=>x.CountryName).ToList();
// IQueryable countries = Country.GetCountries();
if (HttpContext.Request.IsAjaxRequest())
{
return Json(new SelectList(
countries,
"CountryID",
"CountryName"), JsonRequestBehavior.AllowGet
);
}
return View(countries);
}
public ActionResult StateList(int CountryID)
{
IQueryable <State> states= db.States. Where(x => x.CountryID == CountryID);
if (HttpContext.Request.IsAjaxRequest())
return Json(new SelectList(
states,
"StateID",
"StateName"), JsonRequestBehavior.AllowGet
);
return View(states);
}
public ActionResult CountryList()
{
var countries=db.countries.OrderBy(x=>x.CountryName.ToList();
//IQueryable countries=Country.GetCountries();
if(HttpContext.Request.IsAjaxRequest())
{
返回Json(新选择列表(
国家,
“CountryID”,
“CountryName”),JsonRequestBehavior.AllowGet
);
}
返回视图(国家);
}
公共操作结果状态列表(int CountryID)
{
IQueryable states=db.states.Where(x=>x.CountryID==CountryID);
if(HttpContext.Request.IsAjaxRequest())
返回Json(新选择列表(
各国,
“StateID”,
“StateName”),JsonRequestBehavior.AllowGet
);
返回视图(状态);
}
以下是同样包含java脚本的视图文件:
@section scripts {
<script type="text/javascript">
$(function () {
$.getJSON("/Dropdown/Countries/List",function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, country) {
items += "<option value='" + country.Value + "'>" + country.Text + "</option>";
});
$("#Countries").html(items);
});
$("#Countries").change(function () {
$.getJSON("/Dropdown/States/List/" + $("#Countries > option:selected").attr("value"), function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, state) {
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$("#States").html(items);
});
});
});
</script>
}
<h1>@ViewBag.Title</h1>
@using (Html.BeginForm())
{
<label for="Countries">Countries</label>
<select id="Countries" name="Countries"></select>
<br /><br />
<label for="States">States</label>
<select id="States" name="States"></select>
<br /><br />
<input type="submit" value="Submit" />
}
@节脚本{
$(函数(){
$.getJSON(“/Dropdown/Countries/List”),函数(数据){
var items=“-------------------”;
美元。每个(数据、功能(i、国家){
项目+=“”+国家/地区。文本+“”;
});
$(“#国家”).html(项目);
});
美元(“#国家”)。变化(功能){
$.getJSON(“/Dropdown/States/List/”+$(“#Countries>option:selected”).attr(“value”)、函数(数据){
var items=“-------------------”;
$。每个(数据、函数(i、状态){
项目+=“”+状态。文本+“”;
});
$(“#States”).html(项目);
});
});
});
}
@视图包。标题
@使用(Html.BeginForm())
{
国家
州
}
首先,您的操作方法名称是StateList
,它需要一个名为CountryID
的参数。但是您的代码没有使用这样的querystring参数调用StateList
操作方法。所以要解决这个问题
$("#Countries").change(function () {
$.getJSON("@Url.Action("StateList","Home")?CountryID=" +
$("#Countries").val(), function (data) {
var items = "<option>---------------------</option>";
$.each(data, function (i, state) {
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$("#States").html(items);
});
});
还可以包含执行ajax调用的javascript代码吗?在states objectIQueryable states=db.states上调用.ToList()方法。其中(x=>x.CountryID==CountryID).ToList()。。。。。。此代码显示红线是什么阻止您使用
$(“#Countries”).val()代码>?当您调用.ToList()时,它不是IQueryable,而是IEnumerable。只需使用var状态=
public ActionResult StateList(int CountryID)
{
var states = db.States.Where(x => x.CountrId==CountryID).ToList();
//this ToList() call copies the data to a new list variable.
var stateOptions = states.Select(f => new SelectListItem {
Value = f.StateID.ToString(),
Text = f.StateName }
).ToList();
if (HttpContext.Request.IsAjaxRequest())
return Json(stateOptions, JsonRequestBehavior.AllowGet);
return View(states);
}