简体   繁体   中英

Making an Ajax call and returning a boolean value 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 value, how can I do this?

Thanks

Well, probably 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,     
        });
    }

The ActionInfo is just a simple 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM