使用ASP.NET MVC的JsonResult类返回多个对象 [英] Return Multiple Objects Using ASP.NET MVC'S JsonResult Class

查看:209
本文介绍了使用ASP.NET MVC的JsonResult类返回多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用ASP.NET MVC的JsonResult类来实现多个对象....这是一个控制器方法,它返回记录的json对象,但我也想传递计数值....

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

如何从asp.net mvc控制器返回计数以及results ....

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

推荐答案

如何创建匿名类型并对其进行JSON处理?

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

例如

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

然后您可以像以前一样在脚本中评估json字符串,但只需查询评估结果中的Count和Results属性即可.

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

这篇关于使用ASP.NET MVC的JsonResult类返回多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆