帮助Asp.Net MVC ajax请求

时间:2014-05-05 14:54:55

标签: jquery asp.net-mvc json asp.net-mvc-4

我使用的是Asp.net MVC。我有一个页面。当我单击页面上的菜单链接时,我想将部分视图加载到该页面。我写了一个如下代码。

 $("a.submit").click(function () {
        var id = $(this).attr("id");
        var UnitId = id.split("_")[1];
        var categoryId = $("#hdnCategory").val();

        $.blockUI({
            css: { backgroundColor: 'transparent', color: '#fff', border: 'none' },
            message: '<h1><img src="../Content/images/loading.gif" style="width:300px;height:300px" /></h1>'

        });


        $.ajax({
            url: '@rootPath/KaliteDokumanYonetimi/GetDocumentsByUnit',
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            data: '{unitId:'+UnitId+'}',
            success: function (result) {
              //  alert("success");
                $.unblockUI();
            },
            error: function () {
                $.unblockUI();
                alert("sorun oluştu");
            }
        });
    });

    //this is also my controller action code piece.
    public JsonResult GetDocumentsByUnit(string unitId)
    {
        return Json(true,JsonRequestBehavior.AllowGet);
    }

我的代码如上。我的问题是,我不能从Action Method获取unitI值。它是null。在javascript方面,我控制这个值是正确的。如何从Action方法中获取unitId值。 提前谢谢......

1 个答案:

答案 0 :(得分:0)

你的json无效。不要尝试通过连接来构建json字符串。创建一个javascript对象并将其传递给JSON.stringify()

data: JSON.stringify({ unitId : UnitId }),

如果你想发送json,你也应该做POST ....或者将id附加到网址的末尾,并确保你的路由设置正确。