Merve Kaya
Merve Kaya

Reputation: 1249

Return multiple json objects from controller

I return multiple json objects but i don't know how to return that objects. I want to get returned json objects and send them to ajax request. This is my ActionResult:

public ActionResult AutoCompleteEventName(string eventName)
        {
            Event ev = new Event();
            ev.Name = eventName;
            var searchEvent = EventService.Instance.Search(ev);
            var totalCount = EventService.Instance.SearchCount(ev);           
        }

Upvotes: 0

Views: 8682

Answers (3)

Jeyhun Rahimov
Jeyhun Rahimov

Reputation: 3787

If you want to send object's list, you can do it with this way:

var yourObjectList = EventService.Instance.LoadSomeEvents();

List<object> objectList = new List<object>();

foreach (var event in yourObjectList)
{
     objectList.Add(new
                   {
                        id = event.Id,
                        name = event.Name,
                    });
}

return Json(objectList, JsonRequestBehavior.AllowGet);

Upvotes: 1

Suraj
Suraj

Reputation: 395

in controller return result as below

 var returnField = new { searchEvent  = "searchEvent", totalCount = totalCount.ToString() };
return Json(returnField, JsonRequestBehavior.AllowGet);

in Ajax Request

 success: function (data) {
var searchEvent  = data.searchEvent;
var totalCount =data.totalCount 
}

Upvotes: 1

Mahesh Reddy
Mahesh Reddy

Reputation: 356

return Json(new { searchEvent = searchEvent , totalCount  = totalCount }, JsonRequestBehavior.AllowGet)

Upvotes: 1

Related Questions