我有一个接受一个字符串参数的MVC JsonResult方法:MVC JsonResult方法不接受参数
public JsonResult GetDestinations(string countryId)
{
List<Destination> destinations = new List<Destination>();
string destinationsXml = SharedMethods.GetDestinations();
XDocument xmlDoc = XDocument.Parse(destinationsXml);
var d = from country in xmlDoc.Descendants("Country")
from destinationsx in country.Elements("Destinations")
from destination in destinationsx.Elements("Destination")
where (string)country.Attribute("ID") == countryId
select new Destination
{
Name = destination.Attribute("Name").Value,
ID = destination.Attribute("ID").Value,
};
destinations = d.ToList();
return Json(new JsonResult { Data = destinations}, JsonRequestBehavior.AllowGet);
}
有了一个jQuery方法调用方法:
//Fetch Destinations
$("#Country").change(function() {
var countryId = $("#Country > option:selected").attr("value");
$("#Destination").html("");
$("#Resort").html("");
$("#Resort").append($("<option></option>").val(0).html("---Select---"));
$.ajax({
type: "POST",
traditional: true,
url: "/Destinations/GetDestinations",
data: "{countryId:'" + countryId + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
BindDestinationSelect(msg)
}
});
});
然而,JsonResult似乎只接收一个空参数。即使Firebug的显示出一个参数被传递:
JSON countryId “11” 来源 {countryId:'11' }
任何想法?由于
FWIW,'“{countryId:'”+ countryId +“'}”'不是JSON。 JSON需要双引号。当我查看Firebug中的POST请求时,您应该真的使用类似http://www.json.org/json2.js的对象序列化为JSON格式 – R0MANARMY 2010-05-10 15:57:38