我在带有MVC4的ASP.NET中有两个级联下拉列表。我正在使用Ajax调用来调用控制器的JSON方法。JSON方法是返回的值集,我需要的下拉列表所需的值,但它不是在Ajax中。它始终显示未能加载数据的错误消息。
以下是AJAX调用的代码
$("#countryname").change(function () {
if ($("#countryname").val() != "Please select") {
var options = {};
options.url = "/ServeEasy/GetStates";
options.type = "GET";
//options.data = JSON.stringify({ countryID: $("#countryname").val() });
options.data = { countryID: $("#countryname").val() };
//alert($("#countryname").val());
options.dataType = "json";
options.contentType = "application/json";
debugger;
options.success = function (slist) {
$("#statename").empty();
for (var i = 0; i < slist.length; i++) {
$("#statename").append("<option>" + slist[i] + "</option>");
}
$("#statename").prop("disabled", false);
};
options.error = function () { alert("Error retrieving states!"); };
$.ajax(options);
}
else {
$("#statename").empty();
$("#statename").prop("disabled", true);
}
});
和JSON方法为以下
[HttpGet]
public JsonResult GetStates(string countryID)
{
List<SelectListItem> StateList = new List<SelectListItem>();
IEnumerable<FOSEntities.State> slist = new List<FOSEntities.State>();
slist = serveEasyServices.GetStateByCountry(countryID);
return Json(slist, JsonRequestBehavior.AllowGet);
//return Json(new { slist = slist }, JsonRequestBehavior.AllowGet);
}
我已经完成了以下更改,并且可以使用!
在JSON方法上,以下更改
public JsonResult GetStates(string countryID)
{
List<SelectListItem> StateList = new List<SelectListItem>();
var slist = serveEasyServices.GetStateByCountry(countryID).Select(s => new SelectListItem { Value = s.StateID.ToString(), Text = s.State }).ToList<SelectListItem>();
return Json(slist, JsonRequestBehavior.AllowGet);
}
和ajax
$("#countryname").change(function () {
var Id = $("#countryname").val();
if ($("#countryname").val() != "Please select") {
var options = {};
options.url = '@Url.Action("GetStates", "ServeEasy")';
options.type = "GET";
options.data = { countryID: Id };
options.dataType = "json";
options.contentType = "application/json";
debugger;
options.success = function (slist) {
$("#statename").empty();
for (var i = 0; i < slist.length; i++) {
$("#statename").append('<option value="' + slist[i].Value + '">' + slist[i].Text + '</option>');
}
$("#statename").prop("disabled", false);
};
options.error = function () { alert("Error retrieving states!"); };
$.ajax(options);
}
else {
$("#statename").empty();
$("#statename").prop("disabled", true);
}
});
@jaimin非常感谢您的帮助:)
尝试这样,这只是示例
$("#CustomerId").change(function () {
var Id = $("#CustomerId").val();
var options = {};
options.url = '@Url.Action("GetCustomerNameWithId", "Test")';
options.type = "GET";
//options.data = JSON.stringify({ countryID: $("#countryname").val() });
options.data = { CustomerNameId: Id };
//alert($("#countryname").val());
options.dataType = "json";
options.contentType = "application/json";
options.success = function (listItems) {
$("#CustomerNameId").empty();
for (var i = 0; i < listItems.length; i++) {
$("#CustomerNameId").append('<option value="' + listItems[i].Value + '">' + listItems[i].Text + '</option>');
}
};
options.error = function () { alert("Error retrieving states!"); };
$.ajax(options);
});
@Html.DropDownList("CustomerId", (SelectList)ViewBag.CustomerNameID, "--Select--")
@Html.DropDownList("CustomerNameId", new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "-- Select --")
控制器
public JsonResult GetCustomerNameWithId(string CustomerNameId)
{
int _CustomerNameId = 0;
int.TryParse(CustomerNameId, out _CustomerNameId);
var listItems = GetCustomerNameId(_CustomerNameId).Select(s => new SelectListItem { Value = s.CID.ToString(), Text = s.CustomerName }).ToList<SelectListItem>();
return Json(listItems, JsonRequestBehavior.AllowGet);
}
public List<Customer> GetCustomerNameId(int CustomerNameId)
{
// Customer cascade DropDown
using (dataDataContext _context = new dataDataContext())
{
return (from c in _context.Customers
where c.CID == CustomerNameId
select c).ToList();
}
}