user2067567
user2067567

Reputation: 3813

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

Upvotes: 0

Views: 132

Answers (1)

musefan
musefan

Reputation: 48435

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"

Upvotes: 1

Related Questions