MVC JsonResult方法不接受参数

时间:2010-05-10 15:52:30

标签: c# asp.net-mvc json

我有一个接受一个字符串参数的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似乎只接收null参数。即使Firebug显示正在传递参数:

JSON countryId  “11” 资源 {countryId:'11' }

有什么想法吗?感谢

4 个答案:

答案 0 :(得分:1)

我认为问题出在你实际传递数据的方式上,你是这样做的:

data: "{countryId:'" + countryId + "'}",

实际上,您应该在客户端使用结构;

data: { countryId: countryId },

jQuery应该能够正确处理传递id。正如您现在所做的那样,它正在尝试将JSON发送到服务器,这不是MVC所期望的,它期望具有名称/值对的POST。

您可能还需要考虑jQuery Form Plugin,因为这会使您的Javascript结构序列化为POST数据非常

答案 1 :(得分:1)

啊,经过多次搜索,我发现了失败的原因。

与格式错误的JSON等无关,而是如果您尝试传递JSON值,控制器方法不知道会发生什么事实:

http://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=863

所以在我的情况下,我只是选择传递一个字符串值。

        $("#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,
                success: function (msg) {
                    BindDestinationSelect(msg.Data)
                }
            });

答案 2 :(得分:0)

public JsonResult BindAllData(string Userid)
    {

        List<VoteList> list = new List<VoteList>();
        var indexlist = db.TB_WebSites.ToList();
        int i = 0;
        var countlist = db.Tb_Votes.ToList();
        var VCountList = db.Tb_Votes.ToList();
        foreach (TB_WebSites vt in indexlist)
        {
            bool voted = false;
        }

        return Json(new { List = _list });

    }


    function DataBind() {
        $("#LoadingDatas").show();
        var userid = $("#FBUserId").text();
        //alert('Data : ' + userid);
        var InnerHtml = "";
        $.ajax(
            {
                url: '/Gitex/BindAllData/',
                type: 'POST',
                data: { "Userid": userid },
                dataType: 'json',
                async: true,
                success: function (data) {
                    //alert('Done');
                    //alert(data.List.length);
                    for (var i = 0; i < data.List.length; i++) {

            });

    }

试试这个,它对我有用 jQuery函数 成功:功能(目的地)

答案 3 :(得分:0)

我建议您使用HttpPost属性修饰您的动作 喜欢: -

[HttpPost]
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);
}