0
votes

Using Telerik's Kendo UI, Kendo.Mvc version 2016.3.1028.545

I have a view that contains an html grid like so:

            @(Html.Kendo().Grid<TfUserLoginHistoryReturnModel>()
                .Name("loginHistoryGrid")
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .Read(read => read.Action("LoginHistory_Read", "Administration", new { DateStart = Model.StartDate }))
                    .Model(model => model.Id(p => p.UserID)
                    )
                )
                .Events(events => events.DataBound("onGridDataBound"))
                .Columns(columns =>
                {
                    columns.Bound(p => p.FullName).Title("Full Name");
                    columns.Bound(p => p.Email);
                    columns.Bound(p => p.CompanyName).Title("Company");
                    columns.Bound(p => p.UserType).Title("User Type");
                    columns.Bound(p => p.AcceptedTermsDate).Title("Date Accepted Terms").Format("{0:MM/dd/yyyy}").Width(125);
                    columns.Bound(p => p.LastSuccessfulLogin).Title("Last Login").Format("{0:MM/dd/yyyy}").Width(125);
                    columns.Bound(p => p.NumLogins).Title("Number of Logins");
                    columns.Bound(p => p.TotalTime).Title("Time Logged (minutes)");
                })
                .Pageable()
                .Sortable()
                .Scrollable()
                .Filterable(f => f.Extra(true)
                    .Operators(o => o.ForString(s => s.Clear()
                    .Contains("Contains")
                    .DoesNotContain("Does not contain")
                    .IsEqualTo("Is equal to")
                    .IsNotEqualTo("Is not equal to")
                    .StartsWith("Starts with")
                    .EndsWith("Ends with")
                    .IsEmpty("Is empty")
                    .IsNotEmpty("Is not empty")
                    .IsNull("Is null")
                    .IsNotNull("Is not null "))))
                .HtmlAttributes(new { style = "height:500px;" })
            )

Associated Scripts:

    function onGridDataBound(e) {
        var grid = e.sender;
        if (grid.dataSource.total() == 0) {
            $(grid).hide();
        }
        else {
            $(grid).show();
        }
    }

    function searchUserGrid(e) {
        if ($("#userSearchText").val() == "") {
            $("#loginHistoryGrid").data("kendoGrid").dataSource.filter({ field: "UserId", operator: "equals", value: -1 });
        } else {
            $filter = [{
                "logic": "or",
                "filters": [
                        { field: "FullName", operator: "contains", value: $("#userSearchText").val() },
                        { field: "Email", operator: "contains", value: $("#userSearchText").val() },
                        { field: "CompanyName", operator: "contains", value: $("#userSearchText").val() }
                ]
            }];
            $("#loginHistoryGrid").data("kendoGrid").dataSource.filter($filter);
        }
    }

Here is the Get for that view in the controller:

    [HttpGet]
    public ActionResult UsageReport(string DateStart)
    {
        try
        {
            UserLoginHistoryViewModel m = new UserLoginHistoryViewModel();

            //default is yesterday
            DateTime sdt = DateTime.Now.Date.AddDays(-1);
            if (!string.IsNullOrWhiteSpace(DateStart))
            {
                DateTime.TryParse(DateStart, out sdt);
            }
            m.StartDate = sdt;

            return View(m);
        }
        catch (Exception ex)
        {
            logger.Error("UsageReportGet", ex);
            return RedirectToAction("Index", "Administration");
        }
    }

When I run this with a breakpoint and go to that page, it doesn't even hit LoginHistory_Read in the controller. While hitting the grid, it errors out. Here is the error and stack trace:

System.ArgumentException was unhandled by user code
HResult=-2147024809 Message=An item with the same key has already been added. Source=mscorlib StackTrace: at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary'2.Insert(TKey key, TValue value, Boolean add) at System.Web.Routing.RouteValueDictionary.Add(String key, Object value) at Kendo.Mvc.UI.GridBoundColumn'2.CreateHeaderBuilderCore() at Kendo.Mvc.UI.GridColumnBase'1.CreateHeaderBuilder() at Kendo.Mvc.UI.Html.GridCellBuilderFactory.CreateHeaderCellBuilder(IGridColumn column) at System.Linq.Enumerable.WhereSelectEnumerableIterator'2.MoveNext() at Kendo.Mvc.UI.Html.GridRowBuilder.CreateRow() at Kendo.Mvc.UI.Html.GridHeaderRowBuilder.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() at Kendo.Mvc.UI.Html.GridDataSectionBuilder.CreateHeader(GridRenderingData data) at Kendo.Mvc.UI.Html.GridScrollingHtmlBuilder.CreateHeader(GridRenderingData renderingData) at Kendo.Mvc.UI.Html.GridScrollingHtmlBuilder.AppendData(IHtmlNode div, GridRenderingData renderingData) at Kendo.Mvc.UI.Html.GridHtmlBuilder.CreateGrid(IDictionary'2 htmlAttributes, GridFunctionalData functionalData, GridRenderingData renderingData) at Kendo.Mvc.UI.Grid'1.WriteHtml(HtmlTextWriter writer) at Kendo.Mvc.UI.WidgetBase.ToHtmlString() at Kendo.Mvc.UI.Fluent.WidgetBuilderBase'2.ToHtmlString() at System.Web.HttpUtility.HtmlEncode(Object value) at System.Web.WebPages.WebPageBase.Write(Object value) at ASP._Page_Views_Administration_UsageReport_cshtml.Execute() in C:\Users\tsimpson\Source\Workspaces\CustomerPortal\CEConnect\CEConnect\Views\Administration\UsageReport.cshtml:line 42 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.RunPage() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList'1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList'1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
InnerException:

So if I run it without breakpoints, the page will load, but the grid is empty.

I have no idea how to know what it is trying to add to the dictionary at that point. I compared this to other grids in my site that do work, and don't see what it could be that's causing this issue.

This is a video I made of what walking through it looks like so you can see exactly at which point the error comes up - hopefully that helps. Be sure to set it to 1080p! Kendo Grid Error

Let me know what other information is needed to try and determine what it could be. Thank you!

2
Is it loading fine for first time?Akash KC
@AkashKC - No. If I run it without breakpoints, the page will load, but because the grid errors before hitting the Read function there's nothing in it.Andy
Could you please share your controller implementation too?Akash KC
@AkashKC - I added the Get for it.Andy
Try with changing your action to public ActionResult UsageReport([DataSourceRequest] DataSourceRequest request,string DateStart)Akash KC

2 Answers

0
votes

So although I admittedly don't fully understand why this wasn't working, I got around it by reworking how the grid gets its data. Instead of setting the grid's DataSource properties and using a Read function, I specify the model for the grid like so: @(Html.Kendo().Grid(Model.LoginHistories) And I added "LoginHistories" as a property of the model my view is using. So in the Get function, I just set that property similar to what the Read function was doing.

0
votes

In my case, my MODEL had 2 properties with similar name:

public int? corporateId { get; set; }
public int? CorporateId { get; set; }

One was CamelCase, and other wasn't.

Solution was simply to remove one or change the name of one of them:

//public int? corporateId { get; set; }
public int? corporate_id { get; set; }
public int? CorporateId { get; set; }