简体   繁体   中英

Return two values back to Ajax from controller

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
  }

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){
               //....
            }
public JsonResult FunctionName()
{
      // do your coding
var result=Json(new {param1 = val1 , param2 = val2});
      return Json(result, JsonRequestBehavior.AllowGet);
}

Get value in Json as:

var val1 = response.Data.param1;

var val2 = response.Data.param2;

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