Asp.net mvc 关于使用jquery$.post将数据过帐到控制器的问题
我有两个下拉列表,一个是国家,另一个是州。第一次表单渲染时,填充“国家”下拉列表,并禁用“状态”下拉列表。当用户选择country时,jquery会出现一个部分post,并调用一个控制器操作方法,该方法返回json数据。问题发生在用户选择国家时,然后我看到我的重载索引方法被调用,它返回json,但firebug显示了类似网络错误的错误 这里我给出了我的控制器代码和cshtml代码。请告诉我哪里出了问题Asp.net mvc 关于使用jquery$.post将数据过帐到控制器的问题,asp.net-mvc,Asp.net Mvc,我有两个下拉列表,一个是国家,另一个是州。第一次表单渲染时,填充“国家”下拉列表,并禁用“状态”下拉列表。当用户选择country时,jquery会出现一个部分post,并调用一个控制器操作方法,该方法返回json数据。问题发生在用户选择国家时,然后我看到我的重载索引方法被调用,它返回json,但firebug显示了类似网络错误的错误 这里我给出了我的控制器代码和cshtml代码。请告诉我哪里出了问题 cshtml code @{ ViewBag.Title = "Home Page"
cshtml code
@{
ViewBag.Title = "Home Page";
}
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/jscript"></script>
<script type="text/jscript">
$(function () {
// $('#CountryID').change(function () {
// var URL = $('#FormID');
// alert(URL);
//// $.getJSON('/DDL/DistrictList/' + $('#State').val(), function (data) {
// });
// });
$('#CountryID').change(function () {
var dropdownID = $(this).val();
$.post('@Url.Action("Index", "Home")', { CountryID: dropdownID }, function (result) {
// result contains your State-List
var items = '<option>--Select State--</option>';
$.each(data, function (i, state) {
alert(i);
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$('#State').html(items);
});
});
});
</script>
@using (Html.BeginForm())
{
<fieldset>
<legend>DropDownList</legend>
@Html.Label("Country")
@Html.DropDownList("Country", ViewBag.Country as SelectList, "--Select Country--", new { id = "CountryID" })
@Html.Label("State")
@Html.DropDownList("State", ViewBag.Country as SelectList, "--Select State--", new { id = "StateID", @disabled = "disabled" })
<input type="submit" value="Create" id="SubmitId" />
</fieldset>
}
controller code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CascadeTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to Cascade Test";
ViewBag.Country = new SelectList(CascadeTest.Models.Country.GetCountries(), "CountryID", "Name");
var State = from s in CascadeTest.Models.State.GetState()
where s.CountryID == ""
select s.Name;
ViewBag.State = new SelectList(State, "StateID", "Name");
return View();
}
[HttpPost]
public JsonResult Index(string CountryID)
{
var State = from s in CascadeTest.Models.State.GetState()
where s.CountryID == CountryID
select s.Name;
string xxx = Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet).ToString();
return Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet);
}
}
}
my model code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CascadeTest.Models
{
public class Country
{
public string CountryID { get; set; }
public string Name { get; set; }
public static IQueryable<Country> GetCountries()
{
return new List<Country>
{
new Country {
CountryID = "CA",
Name = "Canada"
},
new Country{
CountryID = "US",
Name = "United States"
},
new Country{
CountryID = "UK",
Name = "United Kingdom"
},
new Country{
CountryID = "IN",
Name = "India"
}
}.AsQueryable();
}
}
public class State
{
public string StateID { get; set; }
public string Name { get; set; }
public string CountryID { get; set; }
public static IQueryable<State> GetState()
{
return new List<State>
{
// State declare for india
new State {
StateID = "WB",
Name = "West Bengal"
,CountryID="IN"
},
new State{
StateID = "BH",
Name = "Bihar"
,CountryID="IN"
},
new State{
StateID = "OR",
Name = "Orissa"
,CountryID="IN"
},
new State{
StateID = "MD",
Name = "Madhya Pradesh"
,CountryID="IN"
},
// State declare for USA
new State{
StateID = "AL",
Name = "Alabama"
,CountryID="US"
},
new State{
StateID = "FL",
Name = "Florida"
,CountryID="US"
},
new State{
StateID = "IA",
Name = "Iowa"
,CountryID="US"
},
new State{
StateID = "MS",
Name = "Mississippi"
,CountryID="US"
},
// State declare for UK
new State{
StateID = "AV",
Name = "Avon"
,CountryID="UK"
},
new State{
StateID = "BS",
Name = "Buckinghamshire"
,CountryID="UK"
},
new State{
StateID = "CH",
Name = "Cheshire"
,CountryID="UK"
},
new State{
StateID = "DS",
Name = "Dorset"
,CountryID="UK"
}
//No state define for canada
}.AsQueryable();
}
}
}
500错误代码意味着很可能在控制器的操作方法中生成了运行时错误。如果您实际检查Firebug的
Net
选项卡中的响应,它应该会告诉您错误是什么
例如,在即兴情况下,我可以看到您没有任何检查来确定查询是否实际返回了任何
状态。它可能是空的,如果它是空的,那么你会得到运行时错误,结果是到处都是“空属性没有此方法”。HomeController上的索引方法是什么样子的?给出了索引方法……请看一看。谢谢你的小费。我是mvc新手,经常犯错误。如果你运行我的代码,并告诉确切的错误,那么它将是非常有用的。thanksi已经看到了net选项卡,并看到所有文件都在客户端正确下载。然后,当我更改国家/地区下拉列表时,问题就出现了。我认为问题可能在于js代码。plzz指南。谢天谢地,500错误意味着错误是服务器端的。如果需要任何帮助,您需要向我们展示HomeController上的Index方法。调试您的Index操作,并确保一切都是您认为应该的(例如,状态
不为null)。将断点放在操作的开头,并逐行遍历以查看失败的位置。这是这里的基本调试过程,在您来到这里之前,您应该已经完成了。
$('#CountryID').change(function () {
var dropdownID = $(this).val();
$.post('@Url.Action("Index", "Home")', { CountryID: dropdownID }, function (result) {
// result contains your State-List
var items = '<option>--Select State--</option>';
$.each(data, function (i, state) {
alert(i);
items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
});
$('#State').html(items);
});
});
public ActionResult Index()
{
ViewBag.Message = "Welcome to Cascade Test";
ViewBag.Country = new SelectList(CascadeTest.Models.Country.GetCountries(), "CountryID", "Name");
var State = from s in CascadeTest.Models.State.GetState()
where s.CountryID == ""
select s.Name;
ViewBag.State = new SelectList(State, "StateID", "Name");
return View();
}
[HttpPost]
public JsonResult Index(string CountryID)
{
var State = from s in CascadeTest.Models.State.GetState()
where s.CountryID == CountryID
select s.Name;
string xxx = Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet).ToString();
return Json(new SelectList(State.ToArray(), "StateID", "Name"), JsonRequestBehavior.AllowGet);
}