This is a migrated thread and some comments may be shown as answers.

Client-side bind to JsonResult

2 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 03 Feb 2015, 10:03 PM
Hi, I'm trying to bind my grid to an MVC JsonResult.  The MVC action is getting hit and returning JSON that I can see in Fiddler.  I'm just trying to return one row right now just to verify it will work. The grid ends up 10 empty rows and no data.  Any assistance would be appreciated.  Below is my databinding section from grid and my MVC JsonResult action method.


<DataBinding SelectMethod="GetAttachmentData" Location="/FaceToFaceAttachmentUpload" SortParameterType="List" FilterParameterType="List" ResponseType="JSONP"/>



[WebMethod]
public JsonResult GetAttachmentData(int startRowIndex, int maximumRows, string sortExpression, string filterExpression)
{
List<IAttachment> attachments = new List<IAttachment>();
attachments.Add(new Attachment() { ID = 1, TypeDesc = "test" });
return Json(new { results = attachments });
}

2 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 06 Feb 2015, 11:41 AM
Hi Bob,

I assume that the GetAttachmentData is not fire at all and that is the reason for not displaying the data in the grid. Please check out whether the locations is correct and whether the method is executed. For your convenience I prepared a small sample and attached it to this thread. Please give it a try and let me know about the result.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Bob
Top achievements
Rank 1
answered on 06 Feb 2015, 05:28 PM
Thanks for reply.  The fix was to name the list of objects as "Data" and I also had to include a object count named "Count".  If these two named parameters weren't returned back in JSON, then the grid would not bind.

Here's the updated method that works as desired:

public JsonResult GetAttachmentData(int startRowIndex, int maximumRows, string sortExpression, int episodeId, int clientId)
{
List<IAttachment> attachments = new List<IAttachment>();

if (episodeId > 0 || clientId > 0)
{
attachments.AddRange(new AttachmentData().GetEncounterAttachments(episodeId, clientId));
if (string.IsNullOrEmpty(sortExpression))
{
attachments.OrderBy(a => a.ClientFileName);
}
else
{
// Uses System.Linq.Dynamic for dynamic sort
// sortExpression comes back from Telerik grid in format "TypeDesc ASC, ClientFileName DESC"
attachments = attachments.OrderBy(sortExpression).ToList();
}
}

return this.Json(new { Count = attachments.Count, Data = attachments.Skip(startRowIndex).Take(maximumRows) });
}
Tags
Grid
Asked by
Bob
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Bob
Top achievements
Rank 1
Share this question
or