已经添加了Kendo UI Html Grid'具有相同密钥的项目'

时间:2017-10-20 19:56:50

标签: asp.net-mvc kendo-ui kendo-grid

使用Telerik的Kendo UI,Kendo.Mvc版本2016.3.1028.545

我有一个包含html网格的视图:

            @(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;" })
            )

相关脚本:

    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);
        }
    }

以下是控制器中的获取视图:

    [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");
        }
    }

当我使用断点运行并转到该页面时,它甚至不会在控制器中点击LoginHistory_Read。在击中网格时,它会出错。这是错误和堆栈跟踪:

  

System.ArgumentException未被用户代码
处理   HResult = -2147024809消息=具有相同键的项已经存在   已被添加。 Source = mscorlib StackTrace:          在System.ThrowHelper.ThrowArgumentException(ExceptionResource资源)          在System.Collections.Generic.Dictionary&#39; 2.Insert(TKey键,TValue值,布尔加法)          在System.Web.Routing.RouteValueDictionary.Add(String key,Object value)          在Kendo.Mvc.UI.GridBoundColumn&#39; 2.CreateHeaderBuilderCore()          在Kendo.Mvc.UI.GridColumnBase&#39; 1.CreateHeaderBuilder()          在Kendo.Mvc.UI.Html.GridCellBuilderFactory.CreateHeaderCellBuilder(IGridColumn)   柱)          在System.Linq.Enumerable.WhereSelectEnumerableIterator&#39; 2.MoveNext()          在Kendo.Mvc.UI.Html.GridRowBuilder.CreateRow()          在Kendo.Mvc.UI.Html.GridHeaderRowBuilder.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          在Kendo.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()          at Kendo.Mvc.UI.Html.GridDataSectionBuilder.CreateHeader(GridRenderingData   数据)          在Kendo.Mvc.UI.Html.GridScrollingHtmlBuilder.CreateHeader(GridRenderingData)   renderingData)          在Kendo.Mvc.UI.Html.GridScrollingHtmlBuilder.AppendData(IHtmlNode div,   GridRenderingData renderingData)          在Kendo.Mvc.UI.Html.GridHtmlBuilder.CreateGrid(IDictionary&#39; 2 htmlAttributes,GridFunctionalData functionalData,GridRenderingData   renderingData)          在Kendo.Mvc.UI.Grid&#39; 1.WriteHtml(HtmlTextWriter writer)          在Kendo.Mvc.UI.WidgetBase.ToHtmlString()          在Kendo.Mvc.UI.Fluent.WidgetBuilderBase&#39; 2.ToHtmlString()          在System.Web.HttpUtility.HtmlEncode(对象值)          在System.Web.WebPages.WebPageBase.Write(Object value)          在ASP._Page_Views_Administration_UsageReport_cshtml.Execute()中   C:\ Users \用户tsimpson \来源\工作区\ CustomerPortal \ CEConnect \ CEConnect \视图\管理\ UsageReport.cshtml:行   42          在System.Web.WebPages.WebPageBase.ExecutePageHierarchy()          在System.Web.Mvc.WebViewPage.ExecutePageHierarchy()          在System.Web.WebPages.StartPage.RunPage()          在System.Web.WebPages.StartPage.ExecutePageHierarchy()          在System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext   pageContext,TextWriter writer,WebPageRenderingBase startPage)          在System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object instance)          在System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer)          在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext   controllerContext,ActionResult actionResult)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList&#39; 1)   filters,Int32 filterIndex,ResultExecutingContext preContext,   ControllerContext controllerContext,ActionResult actionResult)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList&#39; 1)   filters,Int32 filterIndex,ResultExecutingContext preContext,   ControllerContext controllerContext,ActionResult actionResult)
  的InnerException:

因此,如果我在没有断点的情况下运行它,页面将加载,但网格为空。

我不知道如何知道它在那时要添加到词典中的内容。我将其与我网站上其他有效的网格进行了比较,并且看不出会导致此问题的原因。

这是我制作的视频,通过它看起来就是这样,你可以准确地看到错误出现在哪一点 - 希望这会有所帮助。一定要设置为1080p! Kendo Grid Error

让我知道需要哪些其他信息才能确定它可能是什么。谢谢!

2 个答案:

答案 0 :(得分:0)

因此,虽然我不能完全理解为什么这不起作用,但我通过重新设计网格如何获取数据来解决这个问题。我没有设置网格的DataSource属性并使用Read函数,而是指定网格的模型,如下所示: @(Html.Kendo()。网格(Model.LoginHistories) 我添加了#34; LoginHistories&#34;作为我的视图正在使用的模型的属性。所以在Get函数中,我只是设置了类似于Read函数所做的属性。

答案 1 :(得分:0)

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; }