简体   繁体   English

ASP.Net C#MCV-将值从Ajax jQuery传递到控制器

[英]ASP.Net C# MCV - Pass value from Ajax Jquery to Controller

I need to pass a value from the front end to the controller, I'm struggling to get it to pass the value. 我需要将一个值从前端传递给控制器​​,但我正努力让它传递值。

Ajax/Jquery Ajax / 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);
                }
            });
        }
    });

Here is the ActionResult. 这是ActionResult。 I've got the code simply putting a comment in the console so I can see that it's worked for now. 我得到的代码只是在控制台中添加了注释,因此我可以看到它现在已经起作用。

[HttpPost]
    public ActionResult UnlockUser(string userid)
    {
        if (userid != "")
        {
            return Json("success - call from HomeController", JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json("error", JsonRequestBehavior.AllowGet);
        }
    }

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

The controller action is decorated with HttpPost but you're sending a GET request in the ajax , change the type to type: 'POST', . 控制器动作以HttpPost装饰,但是您要在ajax发送GET请求,将类型更改为type: 'POST',

You don't need to append data to your query string when issuing a POST request. 发出POST请求时,无需将数据附加到查询字符串中。

Also, if you're specifying application/json make sure you send json , at the moment you're sending a string. 另外,如果要指定application/json确保在发送字符串的同时发送json So, you could either remove the line contentType: "application/json; charset=utf-8", or change the data parameter to data: JSON.stringify({ userid: userid }) . 因此,您可以删除行contentType: "application/json; charset=utf-8",或将data参数更改为data: JSON.stringify({ userid: userid })

Your ajax request could look like: 您的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);
            }
        });

Or 要么

    $.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);
            }
        });

Try something like this, this will work: 尝试这样的事情,这将起作用:

$("#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);
        }
    });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net mcv:如何将javascript数组发送到c#控制器 - asp.net mcv: How to send javascript array to c# controller jQuery传递给MVC asp.net C#控制器 - jQuery to pass to MVC asp.net c# controller 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC 如何通过Jquery.ajax()将字符串值从视图传递到ASP.NET MVC控制器 - How to pass string value from a view via Jquery.ajax() to ASP.NET MVC controller 将值从Button传递回ASP.NET MVC 3 Razor页面的C#控制器 - Pass Value from Button back to C# Controller from ASP.NET MVC 3 Razor Page 如何在 c#asp.net mvc 中从控制器获取会话变量值到 ajax 调用 - How to fetch Session variable value from a controller to ajax call in c# asp.net mvc C#ASP.net MVC Ajax MySQL DATATABLES列搜索无法将搜索值传递给Controller - C# ASP.net MVC Ajax MySQL DATATABLES Column searching can't pass search value to Controller 将参数从Jquery传递到webhandler asp.net C# - Pass parameter from Jquery to webhandler asp.net c# 从jQuery / JavaScript / AJAX检索一个值以添加到数据库(ASP.NET/C#) - Retrieve a value from jQuery/JavaScript/AJAX to add to database (ASP.NET/C#) 在C#ASP.NET中将值从视图传递到视图模型 - Pass Value from view to viewmodel in c# ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM