我有一個接受一個字符串參數的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