Return Multiple Objects Using ASP.NET MVC'S JsonResult Class

懵懂的女人 提交于 2019-12-21 05:06:09

问题


Is it possible to Multiple Objects Using ASP.NET MVC'S JsonResult Class.... Here is a controller method which returns json object of my records but i also want to pass the count value....

var materials = consRepository.FindAllMaterials().AsQueryable();
var count = materials.Count();
var results = new PagedList<MaterialsObj>(materials, currentPage-1, pageSize);
return Json(results);

How to return count along with the results from asp.net mvc controller....


回答1:


How about creating an anonymous type and JSON'ing that?

e.g.

var resultCount = results.Count;
var genericResult = new { Count = resultCount, Results = results };
return Json(genericResult);

You can then eval your json string in your script as before but just query the Count and Results properties on your eval result.




回答2:


There is a way to send multiple objects which are dynamically identified to send. See this.




回答3:


In C# part:

Using new keywork

var genericResult = new { homeworkData = homework, attachmentData = homeworkAttachment };
var result = this.Json(genericResult, JsonRequestBehavior.AllowGet);
return result;

In jquery side :

function getHomewrokDetailResponse(dataSent, result) {
if (result && result.homeworkData) {
    homeworkId = result.homeworkData.homeworkId;

    ....
}
 if (result && result.attachmentData) {
    xy = result.attachmentData.xyz;

    ....
}


来源:https://stackoverflow.com/questions/2765082/return-multiple-objects-using-asp-net-mvcs-jsonresult-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!