Kendo grid not show data - Asp net core

1 Answer 159 Views
Grid
Lorusso
Top achievements
Rank 1
Lorusso asked on 13 Jun 2023, 02:01 PM

Hi All,
I have a problem. I've a not working grid. I do a compilated query for get the data but the data never gets loaded in the grid.

What is the error? Thanks in advance for any help.

Content of controller 
using DataLayerIAccess.Repository;
using DataLayerIAccess.Repository.Queries;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using WebViewerIAccess.Models;

namespace WebViewerIAccess.Controllers
{
    public class GridController : Controller
    {
        private readonly IAccessDbContext _context;


        public GridController(IAccessDbContext context)
            => _context = context;


        public async Task<IActionResult> ElencoPresenze_Read([DataSourceRequest] DataSourceRequest request)
        {
            var elencoPresenze = (await _context.GetElencoPresenze(0, 10)).ToList();
            var presenzeViewModel = Utilities.MapFromAType<PresenzeModel>(elencoPresenze);

            var dsResult = presenzeViewModel.ToDataSourceResult(request);
            return Json(dsResult);
        }

Content of .cshtml

@{
    ViewData["Title"] = "Presenze";
}

<div class="text-center">
       @(Html.Kendo().Grid<WebViewerIAccess.Models.PresenzeModel>()
        .Name("grid")
        .Columns(c => {
            c.Bound(p => p.ID);
            c.Bound(p => p.Nome);
            c.Bound(p => p.Cognome);
            c.Bound(p => p.Giorno);
            c.Bound(p => p.Reparto);
        })        .Pageable(p => p
            .PageSizes(new[] { 10, 20, 50 })
            .Refresh(true)
            .Responsive(true))
            .Sortable()
        .Filterable()
        .Scrollable()
        .Groupable()
        .DataSource(ds => ds
            .Ajax()
            .Batch(true)
            .PageSize(20)
            .AutoSync(true)
            .ServerOperation(false)
            .Events(e => e.Error("error_handler"))
            .Model(m => {
                m.Field(p => p.ID);
                m.Field(p => p.Nome);
                m.Field(p => p.Cognome);
            })
            .AutoSync(true)
            .Read(r => r.Action("ElencoPresenze_Read", "Grid"))
</div>

<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>

Content of Model
using System;

namespace WebViewerIAccess.Models
{
    public class PresenzeModel
    {
        public int ID { get; set; }
        public string Nome { get; set; }
        public string Cognome { get; set; }
        public DateTime Giorno { get; set; }
        public string Reparto { get; set; }
    }
}

 

thank you

      

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 16 Jun 2023, 06:42 AM

Hi Lorusso,

Inspecting the provided snippets the configuration of the Grid seem correct. Are there any JavaScript errors logged on the console? Is the data returned to the View? Can you inspect the Network tab and verify if data is returned? If data is returned successfully can you share the response format? Also make sure the JSON serialization options are configured. 

On a side note I should mention that there is an ToDataSourceResultAsync method that you can use for asynchronous calls to the action methods. Check this article for further details.

Regards,
Aleksandar
Progress Telerik

As of R2 2023, the default icon type will be SVG instead of Font. See this blogpost for more information.
Tags
Grid
Asked by
Lorusso
Top achievements
Rank 1
Answers by
Aleksandar
Telerik team
Share this question
or