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

Unable to bind Asp.Net Core Grid in MVC

4 Answers 346 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nhir
Top achievements
Rank 1
Nhir asked on 13 Mar 2017, 02:03 PM

Hi,

I am new to Telerik controls. I am trying to bind grid to datasource, not sure what am I doing wrong but grid is always empty.

Here is some sample code.

 public IActionResult Index(string YYYYQ)
        {
            var initialYYYYQ = YearAndMonth.AbstractStartYearMonth();
            SelectedMeasuresViewModel sModel = GetMeasureSelectionData(initialYYYYQ);      
            return View(sModel);
        }

 public ActionResult MeasureSelections_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(GetMeasureSelectionData("2017-02"));
        }

private SelectedMeasuresViewModel GetMeasureSelectionData(string YYYYMM)
        {

         measureSelectionList = someData;
            SelectedMeasuresViewModel sModel = new SelectedMeasuresViewModel();
            sModel.SelectedYYYYMM = YYYYMM;
            sModel.SelectedMeasureSets = measureSelectionList; (this is list of MeasureListData)

            return sModel;
        }

 

View is simple

@using (Html.BeginForm())
{

 @Html.DropDownListFor(m => m.SelectedYYYYMM, YearAndMonth.MonthDropdownOptions(), new { onchange = "this.form.submit(); " })

@Html.Partial("MeasureSelections", Model.SelectedMeasureSets)

}

Partial View is

@(Html.Kendo().Grid<EncorI.Web.Areas.Admin.Models.MeasureListData>()
        .Name("measureSelectionGrid")
        .Columns(columns =>
        {
            columns.Bound(c => c.Enabled).Width(100);
            columns.Bound(c => c.MeasureSetID).Width(100);
            columns.Bound(c => c.MeasureSetName).Width(300);
            columns.Bound(c => c.HospitalID).Width(100);
            columns.Bound(c => c.HospitalName).Width(300);
        })
        .HtmlAttributes(new { style = "height: 850px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("MeasureSelections_Read", "measureSelectionGrid"))
            .PageSize(20)
        )
)

4 Answers, 1 is accepted

Sort by
0
Nhir
Top achievements
Rank 1
answered on 13 Mar 2017, 03:34 PM
Nm figured it out.
0
Dean
Top achievements
Rank 1
answered on 09 Jul 2017, 02:29 AM

As someone now having the same problem, any chance you can actually post what the problem was?

My code is almost identical to yours, other than returning as ToDataSourceResult(request) and my grid refuses to display any rows.

 

0
Dean
Top achievements
Rank 1
answered on 09 Jul 2017, 02:46 AM

In case anyone runs in to the same problem. For me it was because I hadn't added this to the startup.cs file.

services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

The page at http://docs.telerik.com/aspnet-core/getting-started/getting-started mentions adding the call to services.AddKendo() and includes the above json in the code example but doesn't specifically say you need to add it (so I ignored it).

 

0
Viktor Tachev
Telerik team
answered on 12 Jul 2017, 02:02 PM
Hi,

The behavior you are observing is because by default in .NET Core the fields will be serialized in camelCase. However, the Grid is looking for fields that are in PascalCase, Thus, the data is not recognized and is not displayed. This is what the setting is doing - it specifies how the data will be serialized to the client. 

For more information on this you can check out the resources below:



Regards,
Viktor Tachev
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Nhir
Top achievements
Rank 1
Answers by
Nhir
Top achievements
Rank 1
Dean
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or