ASP.Net C#MCV - 将值从Ajax Jquery传递给Controller

问题描述 投票:0回答:3

我需要将一个值从前端传递给控制器​​,我很难让它传递值。

阿贾克斯/ jQuery的

//unlock user account
    $("#results").on('click', ".unlockactionbutton", function (e) {
        e.preventDefault();
        var userid = this.getAttribute("userid");
        if (envdd.children(':selected').val() == "") {
            alert("Please select User"); 
        } else {
            alert(userid);
            $.ajax({
                type: "GET",
                url: "@Url.Action("UnlockUser", "Home", new { userid = userid })",
                //Url.Action("UnlockUser", "Home", new { id = userid });
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(userid),
                success: function (data) {
                    console.log(data);
                },
                error: function(data) {
                    alert('error');
                    console.log(data);
                }
            });
        }
    });

这是ActionResult。我有代码只是在控制台中发表评论,所以我可以看到它现在有效。

[HttpPost]
    public ActionResult UnlockUser(string userid)
    {
        if (userid != "")
        {
            return Json("success - call from HomeController", JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json("error", JsonRequestBehavior.AllowGet);
        }
    }
c# asp.net asp.net-mvc asp.net-ajax
3个回答
1
投票

控制器动作用HttpPost装饰,但你在ajax发送一个GET请求,将类型改为type: 'POST',

发出POST请求时,无需将数据附加到查询字符串。

此外,如果您指定application/json,请确保在发送字符串时发送json。因此,您可以删除行contentType: "application/json; charset=utf-8",或将数据参数更改为data: JSON.stringify({ userid: userid })

您的ajax请求可能如下所示:

    $.ajax({
            type: "POST",
            url: "@Url.Action("UnlockUser", "Home")",  
            dataType: "json",
            data: JSON.stringify(userid),
            success: function (data) {
                console.log(data);
            },
            error: function(data) {
                alert('error');
                console.log(data);
            }
        });

要么

    $.ajax({
            type: "POST",
            url: "@Url.Action("UnlockUser", "Home")",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ "userid" : userid }),
            success: function (data) {
                console.log(data);
            },
            error: function(data) {
                alert('error');
                console.log(data);
            }
        });

3
投票

在您的ajax请求中,您使用的是动词GET,您的操作是POST方法。


1
投票

尝试这样的事情,这将有效:

$("#results").click(function (){
    var userid = this.getAttribute("userid");
    $.ajax({
        type: "POST",
        url: "/Home/UnlockUser",
        "data": "{userid:'" + userid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
             console.log(data);
        }
    });
})
© www.soinside.com 2019 - 2024. All rights reserved.