Making an Ajax call and returning a boolean in an ASP.NET MVC application

I want to make an ajax call (using jQuery) in an ASP.NET MVC application and return a boolean, how can I do this?

thanks

+3
source share
1 answer

Well, perhaps the best solution is to use JSON serialization.

public ActionResult DoSomething(string parameter)
    {
        //do something with parameter
        bool result = true;
        return Json(new ActionInfo()
        {
            Success =result,     
        });
    }

ActionInfo is just a class with one property, boolean Success.Then, jquery ajax call:

$.ajax({
type: "POST",
url: "YourController/DoSomething?parameter=pValue",
data: {},
dataType: "json",
success: function(actionInfo) {

    alert(actionInfo.Success);

}});

Hope this helps.

+13
source

Source: https://habr.com/ru/post/1720044/


All Articles