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

[Solved] ForeignKeyColumn An item with the same key has already been added

1 Answer 123 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
StevenDale
Top achievements
Rank 2
StevenDale asked on 15 Mar 2012, 03:07 PM
ASP.Net MVC 2012 Q1
I have a .aspx page that contains a Telerik Grid. Within the details view of my grid, I have a TabStrip. Within each tab there is a Telerik Grid. Within each Grid I have a ForeignKey column. In the first Grid the ForeignKey column points to ViewData["AddressTypes"]. In the second Grid the ForeignKey column points to ViewData["EmailAddressTypes"]. Both of these lists (i.e. ViewData["AddressTypes"] and ViewData["EmailAddressTypes"]) contain the same elements and therefore have the same Ids. When I run the application I get the following error and stack trace. Further below is my markup.

NOTE: This error appears to be the ForeignKeyColumns using same lists in conjunction with having Edit and Delete Commands. If I comment out either the foreignKey columns or the command columns then I get no error.

An item with the same key has already been added.

   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Web.Mvc.ViewDataDictionary.Add(String key, Object value)
   at Telerik.Web.Mvc.UI.GridForeignKeyColumn`2.AppendSelectList(IDictionary`2 viewData, Object dataItem) in f:\114\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Columns\GridForeignKeyColumn.cs:line 96

.DetailView(details2 => details2.ClientTemplate(
                                    Html.Telerik().TabStrip()
                                    .Name("TabStrip_<#= Id #>")
                                    .SelectedIndex(0)
                                    .Items(items2 =>
                                    {
                                        items2.Add().Text("Addresses").Content
                                        (
                                            Html.Telerik().Grid<AddressModel>().Name("UserAddress_<#= Id #>")
                                            .DataKeys(keys => { keys.Add(a => a.Id).RouteKey("addressId"); keys.Add(a => a.OwnerId).RouteKey("addressOwnerId"); })
                                            .Columns(columns =>
                                            {
                                                columns.ForeignKey(a => a.TypeId, (IEnumerable)ViewData["AddressTypes"], "Id", "Name").Width(100);
                                                columns.Bound(a => a.AddressLine1).Width(200);
                                                columns.Bound(a => a.AddressLine2).Width(200);
                                                columns.Bound(a => a.City).Width(120);                                               
                                                columns.ForeignKey(a => a.StateId, (IEnumerable)ViewData["States"], "Id", "Name").Width(100);
                                                columns.Bound(a => a.ZipCode).Width(100);
                                                columns.Command(commands => { commands.Edit(); commands.Delete(); }).Width(100);                                           
                                            })
 
                                            .DataBinding(dataBinding => dataBinding.Ajax()
                                            .Select("AddressAjax", "Group", new { organizationalUnitId = "<#= Id #>" })
                                            .Insert("InsertAddress", "Group")
                                            .Update("SaveAddress", "Group")
                                            .Delete("DeleteAddress", "Group")
                                            )
                                            .Pageable()
                                            .Sortable()
                                            .Filterable()
                                            .ToHtmlString()
                                        );
                                        items2.Add().Text("Email").Content
                                        (
                                            Html.Telerik().Grid<EmailModel>().Name("UserEmailAddress_<#= Id #>")
                                            .DataKeys(keys => { keys.Add(e => e.Id).RouteKey("emailId"); keys.Add(e => e.OwnerId).RouteKey("ownerId"); })
                                            .Columns(columns =>
                                            {
                                                columns.Bound(e => e.EmailAddress).Width(200);         
                                                columns.ForeignKey(e => e.TypeId, (IEnumerable)ViewData["EmailAddressTypes"], "Id", "Name").Width(100);                                               
                                                columns.Command(commands => { commands.Edit(); commands.Delete(); }).Width(100);                                      
                                            })
                                            .DataBinding(dataBinding => dataBinding.Ajax()
                                                .Select("EmailAddressAjax", "Group", new { organizationalUnitId = "<#= Id #>" })                                                                        
                                                .Insert("InsertEmailAddress", "Group")               
                                                .Update("SaveEmailAddress", "Group")               
                                                .Delete("DeleteEmailAddress", "Group")
                                            )                                         
                                            .Pageable()
                                            .Sortable()
                                            .Filterable()
                                            .ToHtmlString()
                                        );
                                    })
                                    .ToHtmlString()
                                    ))

Thanks,

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 19 Mar 2012, 12:37 PM
Hello Billy,

I posted my answer in the support ticket you have opened. For convenience, I am pasting my reply below:

*******************************************************************************************************************
The problem occurs because the properties have the same name. So when the Grid tries to add the "TypeId_Data" key to the ViewDataDictionary an exception is thrown. The easiest way to workaround this limitation is to remove the item from the ViewContext ViewDataDictionary before the ForeignKey column definition in the second Grid. For example:
columns.Bound(e => e.EmailAddress).Width(200);
  
this.ViewContext.ViewData.Remove("TypeId_Data");
          
columns.ForeignKey(e => e.TypeId, (IEnumerable)ViewData["EmailAddressTypes"], "Id", "Name").Width(100);


*******************************************************************************************************************

Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Grid
Asked by
StevenDale
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Share this question
or