This is a migrated thread and some comments may be shown as answers.
exception: An item with the same key has already been added.
2 Answers 109 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Koen
Top achievements
Rank 1
Koen asked on 08 Apr 2011, 12:40 PM
Dear Support Team :-)

I'm having a blocking problem with the MVC grid.
What I want to archieve should be fairly easy to do, so it surprises me a little,
that I can't manage to get it working.

I want to add a delete button to the grid, but when I add the column:
columns.Command(commands =>
{
    commands.Delete().ButtonType(GridButtonType.Text);
}).Width(90).Title("Commands");

Then the grid throws an exception: An item with the same key has already been added
Stacktrace:
  
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
at Telerik.Web.Mvc.UI.GridDataKeyStore.GetRouteValues(Object dataItem)
at Telerik.Web.Mvc.UI.GridUrlBuilder.DeleteUrl(Object dataItem)
at Telerik.Web.Mvc.UI.Html.GridFormButtonBuilder.Create(Object dataItem)
at Telerik.Web.Mvc.UI.Html.GridActionCellBuilder.AppendCellContent(IHtmlNode td, Object dataItem)
at Telerik.Web.Mvc.UI.Html.GridDataCellBuilderBase.CreateCell(Object dataItem)
at Telerik.Web.Mvc.UI.Html.GridDataRowBuilder.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow()
at Telerik.Web.Mvc.UI.Html.GridDataSectionBuilder.CreateBody(IEnumerable`1 rowBuilders)
at Telerik.Web.Mvc.UI.Html.GridDataSectionBuilder.CreateBody(GridRenderingData renderingData)
at Telerik.Web.Mvc.UI.Html.GridHtmlBuilder.CreateBody(GridRenderingData renderingData)
at Telerik.Web.Mvc.UI.Html.GridHtmlBuilder.AppendData(IHtmlNode div, GridRenderingData renderingData)
at Telerik.Web.Mvc.UI.Html.GridHtmlBuilder.CreateGrid(IDictionary`2 htmlAttributes, GridFunctionalData functionalData, GridRenderingData renderingData)
at Telerik.Web.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer)
at Telerik.Web.Mvc.UI.ViewComponentBase.ToHtmlString()
at Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToHtmlString()
at Telerik.Web.Mvc.{Dynamic}.GridBuilder`1\[\[Ogone.FrontEnd.Dto.NCFrontEnd.SubscriptionOption\, Ogone.FrontEnd.Dto\, Version=1.0.0.0\, Culture=neutral\, PublicKeyToken=null\]\].IHtmlString.ToHtmlString()
at System.Web.HttpUtility.HtmlEncode(Object value)
at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, Object content)
at System.Web.WebPages.WebPageBase.Write(Object value)
at ASP._Page_Views_Subscription_SubscriptionOptions_cshtml.Execute() in c:\Workdir\dotNet\FrontEnd.root\FrontEnd\FrontEnd.Web.BackOffice\Views\Subscription\SubscriptionOptions.cshtml:line 14
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.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)

Now I assume / suspect that it has to do with the multiple primary key that is configured for the datasource.
The primary key consists of a combination of 3 fileds: SubscriptionId, OptionId, Modes.

I've configured this in the following way:
.DataKeys(keys =>
{
    keys.Add(s => s.SubscriptionId);
    keys.Add(s => s.OptionId);
    keys.Add(s => s.Modes);
})

I've read here on this forum that this was the way to do so.
The complete code of the grid is:

@(Html.Telerik().Grid(Model.OptionsLinked)
        .Name("grid")
        .HtmlAttributes(new { @class = "gridTable", width = "100%" })
        .DataKeys(keys =>
        {
            keys.Add(s => s.SubscriptionId);
            keys.Add(s => s.OptionId);
            keys.Add(s => s.Modes);
        })
            .DataBinding(d => d.Server()
                        .Delete("SubscriptionOptionDelete", "Subscription", Html.OGAntiForgeryTokenRouteValues("Subscription/SubscriptionOptionDelete")))
        .Columns(columns =>
        {
            columns.Bound(s => s.OptionId)
                .ClientTemplate(Html.Ogone().ActionLink().Text("Delete").TargetAction("DeleteSubscription").RouteValues(new { Id = "<#= Id #>" }).ToString())
                .Title("Delete")
                .Sortable(false)
                .Filterable(false);
            columns.Command(commands =>
            {
                commands.Delete().ButtonType(GridButtonType.Text);
            }).Width(90).Title("Commands");
            columns.Bound(s => s.SubscriptionId).Visible(false);
            columns.Bound(s => s.OptionId).Width(100);
            columns.Bound(s => s.Modes).Width(100);
            columns.Bound(s => s.MonthlyFee).Width(100);
            columns.Bound(s => s.SetupFee).Width(100);
            columns.Bound(s => s.Currency).Width(100);
            columns.Bound(s => s.ActivByMerchant).Width(100);
            columns.Bound(s => s.ByDefault).Width(100);
            columns.Bound(s => s.Selectable).Width(100);
            columns.Bound(s => s.TXCost).Width(100);
            columns.Bound(s => s.TXRate).Width(100);
            columns.Bound(s => s.MinCost).Width(100);
            columns.Bound(s => s.MaxCost).Width(100);
            columns.Bound(s => s.Cost3DS).Width(100);
            columns.Bound(s => s.Comments).Width(100);
            columns.Bound(s => s.ConfirmationMemoId).ClientTemplate("").Width(100);
        })
        .EnableCustomBinding(true)
        .Filterable()
        .Sortable(sorting =>
        {
            sorting.SortMode(GridSortMode.MultipleColumn);
            //sorting.OrderBy(sortOrder => sortOrder.Add(s => s.Id));
        })
)

Could you help me out with this problem? I don't have a clue what I should do to avoid this error.

Thanks in advance.

Best regards,

Koen Appeltans




2 Answers, 1 is accepted

Sort by
0
Jann
Top achievements
Rank 1
answered on 18 Apr 2011, 10:51 AM
I think you can fix this by specifying a RouteKey like so:
.DataKeys(keys =>
{
    keys.Add(s => s.SubscriptionId).RouteKey("SubscriptionId");
    keys.Add(s => s.OptionId).RouteKey("OptionId");
    keys.Add(s => s.Modes).RouteKey("Modes");
})
0
Koen
Top achievements
Rank 1
answered on 26 Apr 2011, 10:19 AM
Great, this solves the exception.
Thanks for the help!

best regards,

Koen
Tags
Grid
Asked by
Koen
Top achievements
Rank 1
Answers by
Jann
Top achievements
Rank 1
Koen
Top achievements
Rank 1
Share this question
or