【发布时间】:2010-05-10 15:52:29
【问题描述】:
我有一个接受一个字符串参数的 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 国家标识 “11” 资源 {countryId:'11'}
有什么想法吗?谢谢
【问题讨论】:
-
FWIW,
"{countryId:'" + countryId + "'}"不是 JSON。 JSON 需要双引号。你真的应该使用类似json.org/json2.js 的东西将对象序列化为 JSON 格式
标签: c# asp.net-mvc json