birdus
birdus

Reputation: 7514

Value from ajax call always false in MVC action

Why does my MVC action (ASP.NET Core 1.1) always show the value false for this code:

$.ajax({
    url: "/home/SetValue",
    type: "post",
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    data: { "val": true }
});

public class HomeController : Controller
{
    [HttpPost]
    public void SetValue(bool val)
    {
        bool check = val;
    }
}

Solution:

I had to do two things to get it to work.

First, I had to delete the dataType line, as that specifies return type and my action doesn't return anything. Second, the contentType was wrong. I can either use application/x-www-form-urlencoded; charset=UTF-8 or just delete that line. That is the default contenteType and that works in this case.

The capitalization of the url was irrelevant, and no callbacks (e.g., success or error) are necessary. So, this works:

    $.ajax({
        url: "/home/setvalue",
        type: "post",
        data: { "val": true }
    });

I can simplify it even more by using jQuery.post instead of jQuery.ajax. Here is my final simple proof of concept in my first ASP.NET Core app:

// JavaScript
$("#myCheckbox").on("click", function ()
{
    var isChecked = $(this).is(":checked");

    $.post({
        url: "/home/setvalue",
        data: { "val": isChecked }
    });
});

// .NET controller
[HttpPost]
public void SetValue(bool val)
{
    bool check = val;
}

Thanks for all the input.

Upvotes: 0

Views: 1321

Answers (4)

Asif Raza
Asif Raza

Reputation: 1021

dataType: 'json',

Its mean return type of data from server should be json

contentType: 'application/json; charset=utf-8',

Its mean data type should be json before to send it to server in your case you need to convert your data into json stringfy.

Upvotes: 1

univ
univ

Reputation: 737

You need to correct the casing of the controller name so it should be Home instead of home.

Also, in order to bind using JSON in ASP.NET Core you need to use the [FromBody] attribute in your controller action. Check out this blog for more info about it: https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

Finally, add the success callback and it should work.

Hope this helps :)

Upvotes: 0

Majdi Saibi
Majdi Saibi

Reputation: 435

Remove DataType from the Ajax code and add [FromBody] to the SetValue method, like this:

public class HomeController : Controller
{ 
    [HttpPost] public void SetValue([FromBody]bool val)
    {
      bool check = val; 
    } 
}

Upvotes: 3

The_Outsider
The_Outsider

Reputation: 1925

You need to remove the datatype=Json . You can also remove the contentType. Putting success and error helps to check what your result is. This code worked for your condition and the boolean in the SetValue method was true.

$("button").click(function () {
        $.ajax({
            type: "POST",
            url: "/Home/SetValue/",
            data: { "val": true },
            success: function (result) {
                alert('ok');
            },
            error: function (result) {
                alert('error');
            }
        });
    });

Upvotes: 2

Related Questions