Returning multiple objects using the ASP.NET MVC JsonResult class

Is it possible to use several objects using the ASP.NET MVC class JsonResult ... Here is a controller method that returns the json object of my records, but also want to pass the value of count ....

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

How to return the counter together with resultsfrom the asp.net mvc controller ....

+3
source share
3 answers

How to create an anonymous type and JSON'ing?

eg.

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

You can then evaluate your json string in your script as before, but just request the Count and Results properties for your eval result.

+16
source

, . . .

0

In the C # part:

Using a new keyword

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

In the jquery field:

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

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

    ....
}
0
source

Source: https://habr.com/ru/post/1743972/


All Articles