使用ASP.NET MVC的JsonResult类返回多个对象

时间:2022-03-22 17:53:39

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....

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

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....

如何返回计数以及asp.net mvc控制器的结果....

3 个解决方案

#1


16  

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

如何创建一个匿名类型和JSON'?

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.

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

#2


0  

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

有一种方法可以发送动态识别要发送的多个对象。看到这个。

#3


0  

In C# part:

在C#部分:

Using new keywork

使用新的关键字

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

In jquery side :

在jquery方面:

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

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

    ....
}

#1


16  

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

如何创建一个匿名类型和JSON'?

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.

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

#2


0  

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

有一种方法可以发送动态识别要发送的多个对象。看到这个。

#3


0  

In C# part:

在C#部分:

Using new keywork

使用新的关键字

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

In jquery side :

在jquery方面:

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

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

    ....
}