简体   繁体   中英

Sending JSON Data in MVC

Currently am making a jQuery ajax call to MVC method and sending data from Controller in the below format:

["UserInfo ID","User ID"]

Controller code:

     var autoSuggestlist;
      ........
      .
     return Json(autoSuggestlist, JsonRequestBehavior.AllowGet);

Now I want to add another different data like:

[ {"editable":true,"edittype":"integer","index":"userInfoId" ]

How I can send these 2 different datas in Controller to jQuery Ajax

In the below code

$.ajax(
        {
            type: "GET",
            url: "/Home/GetColumnNamesForGrid",
            data: "",
            dataType: "json",
            async: false,
            success: function (result) {

result should get me both the above JSON data. How do I need to modify my Controller Code. Please assist

Thanks

Not sure exactly what you mean, but if you want to sent an object with properties from the controller, you can do this:

return Json(new { editable = true, edittype = "integer", index = "userInfoId" }, JsonRequestBehavior.AllowGet);

Then from javascript, your result object can be used as follows:

var editable = result.editable;//will be true

If you are actually wanting to send both data types back at the same time, then create a wrapper object like so:

var myObject = new { editable = true, edittype = "integer", index = "userInfoId" };
var myArray = autoSuggestlist;

return Json(new { @myObject = myObject, @myArray = myArray}, JsonRequestBehavior.AllowGet);

Then use in javascript like this:

var myObject = result.myObject;
var editable = myObject.editable;//will be true

var myArray = result.myArray;
var firstItem = myArray[0];//will be "UserInfo ID"

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