1

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

//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. 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);
        }
    }
1
  • 3
    your url should be @Url.Action("UnlockUser", "Home") and then do data: { userid : userid } Commented Jan 15, 2019 at 11:46

3 Answers 3

4

On your ajax request you are using the verb GET and your action is a POST method.

Sign up to request clarification or add additional context in comments.

1 Comment

Been trying various different ways of achieving this so left over from another attempt! Silly me
2

The controller action is decorated with HttpPost but you're sending a GET request in the ajax, change the type to type: 'POST',.

You don't need to append data to your query string when issuing a POST request.

Also, if you're specifying application/json make sure you send json, at the moment you're sending a string. So, you could either remove the line contentType: "application/json; charset=utf-8", or change the data parameter to data: JSON.stringify({ userid: userid }).

Your ajax request could look like:

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

1 Comment

Thanks, your first option helped! Makes sense now!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.