【问题标题】:Helping about Asp.Net MVC ajax request [duplicate]帮助有关 Asp.Net MVC ajax 请求 [重复]
【发布时间】:2014-06-21 22:26:46
【问题描述】:

我使用 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 值。它是空的。在 javascript 方面,我控制它正确的这个值。如何从 Action 方法中获取 unitId 值。 提前谢谢...

【问题讨论】:

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


【解决方案1】:

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

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

如果您想发送 json.... 或将 id 附加到 url 的末尾并确保您的路线设置正确,您还应该执行 POST

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多