从控制器返回多个json对象

时间:2014-02-21 07:10:24

标签: javascript ajax asp.net-mvc json asp.net-mvc-4

我返回多个json对象,但我不知道如何返回这些对象。我想获得返回的json对象并将它们发送到ajax请求。这是我的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);           
        }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

如果要发送对象列表,可以这样做:

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);

答案 2 :(得分:0)

在控制器中 返回结果如下

 var returnField = new { searchEvent  = "searchEvent", totalCount = totalCount.ToString() };
return Json(returnField, JsonRequestBehavior.AllowGet);
Ajax请求中的

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