У меня есть метод 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 требует двойных кавычек. Вы действительно должны использовать что-то вроде http://www.json.org/json2.js для сериализации объектов в формате JSON – R0MANARMY