C# – Return two values back to Ajax from controller

ajaxasp.net-mvccjquery

Is it possible to return false and a additional datatype(like int, bool etc) from controller back to Ajax?

 $.post('/Home/SomeAction', { "id": Id }, function (data) {
                if (data) {  
                     ...
                   }else if(data.anotherBool == true){
                   //....
                   }

I need three values for tree different purpose, true, false, and another boolean.

 public JsonResult SomeAction(int id) {

        ........

       return Json(false, anotherBoolean); //<-- what I want
  }

Best Answer

Yes you can do that by using JSON. Try something like this from your controller:

public JsonResult yourFunctionName()
{
      // your code here
      return Json(new {booleanValue = anotherBoolean, intValue = anotherIntValue}, JsonRequestBehavior.AllowGet);
}

Additionally, you can get the values in your Ajax call like this:

$.post('/Home/SomeAction', { "id": Id }, function (data) {
            if (data.intValue == 1) {  
               //....
            }
            else if(data.booleanValue == true){
               //....
            }
Related Topic