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

Editing with Foreign Key Columns

3 Answers 204 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.
Chris McNear
Top achievements
Rank 1
Chris McNear asked on 27 Jan 2012, 03:13 AM
Let me start by saying I love the controls and how easy they make everything.

I have an issue with the gird control using Foreign Key columns, When initially loading data and on refreshes it works beautifully but when I try to use the edit command with a pop up window (built in) I get the following error....

Server Error in '/' Application.
 
An item with the same key has already been added.
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.ArgumentException: An item with the same key has already been added.
 
Source Error:
 
 
Line 6:  <h2>
Line 7:      USer Stories</h2>
Line 8:  @(
Line 9:   Html.Telerik().Grid(Model).Name("ContactGrid").DataKeys(datakey => datakey.Add(c => c.StoryId).RouteKey("StoryId"))
Line 10:         .DataBinding(databinding =>
 
Source File: c:\Users\cmcnear\Documents\Visual Studio 2010\Projects\MWest\MWestRazor\Views\Admin\UserStories.cshtml    Line: 8
 
Stack Trace:
 
 
[ArgumentException: An item with the same key has already been added.]
   System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +9374523
   System.Web.Mvc.ViewDataDictionary.Add(String key, Object value) +15
   Telerik.Web.Mvc.UI.GridForeignKeyColumn`2.AppendSelectList(IDictionary`2 viewData, Object dataItem) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Columns\GridForeignKeyColumn.cs:95
   Telerik.Web.Mvc.UI.Html.<>c__DisplayClass2.<EditorForModel>b__0(Action`2 action) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridHtmlHelper.cs:61
   Telerik.Web.Mvc.Extensions.EnumerableExtensions.Each(IEnumerable`1 instance, Action`1 action) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\Extensions\EnumerableExtensions.cs:61
   Telerik.Web.Mvc.UI.Html.GridHtmlHelper`1.EditorForModel(Object dataItem, String templateName, IEnumerable`1 foreignKeyData, Object additionalViewData) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridHtmlHelper.cs:61
   Telerik.Web.Mvc.UI.Html.<>c__DisplayClass29.<CreateEditFormBuilder>b__26() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderFactory.cs:270
   Telerik.Web.Mvc.UI.Html.GridEditFormBuilder.AppendEditor(IHtmlNode form) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridEditFormBuilder.cs:51
   Telerik.Web.Mvc.UI.Html.GridEditFormBuilder.CreateForm() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridEditFormBuilder.cs:31
   Telerik.Web.Mvc.UI.Html.GridPopUpEditRowBuilder.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridPopUpEditRowBuilder.cs:25
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridRowBuilderDecoratorBase.CreateRow() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridRowBuilderDecoratorBase.cs:39
   Telerik.Web.Mvc.UI.Html.GridDataSectionBuilder.CreateBody(IEnumerable`1 rowBuilders) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridDataSectionBuilder.cs:38
   Telerik.Web.Mvc.UI.Html.GridDataSectionBuilder.CreateBody(GridRenderingData renderingData) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridDataSectionBuilder.cs:29
   Telerik.Web.Mvc.UI.Html.GridHtmlBuilder.CreateBody(GridRenderingData renderingData) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridHtmlBuilder.cs:193
   Telerik.Web.Mvc.UI.Html.GridHtmlBuilder.AppendData(IHtmlNode div, GridRenderingData renderingData) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridHtmlBuilder.cs:68
   Telerik.Web.Mvc.UI.Html.GridHtmlBuilder.CreateGrid(IDictionary`2 htmlAttributes, GridFunctionalData functionalData, GridRenderingData renderingData) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Html\GridHtmlBuilder.cs:43
   Telerik.Web.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer) in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\Grid\Grid.cs:676
   Telerik.Web.Mvc.UI.ViewComponentBase.ToHtmlString() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\ViewComponentBase.cs:215
   Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToHtmlString() in f:\115\Griffin\Trunk Full\Sources\Source\Telerik.Web.Mvc\UI\ViewComponentBuilderBase.cs:154
   Telerik.Web.Mvc.{Dynamic}.GridBuilder`1[[MWestRazor.Models.UserStory, MWestRazor, Version=1.0.0.0\, Culture=neutral\, PublicKeyToken=null\]\].IHtmlString.ToHtmlString() +31
   System.Web.HttpUtility.HtmlEncode(Object value) +38
   System.Web.WebPages.WebPageBase.Write(Object value) +64
   ASP._Page_Views_Admin_UserStories_cshtml.Execute() in c:\Users\cmcnear\Documents\Visual Studio 2010\Projects\MWest\MWestRazor\Views\Admin\UserStories.cshtml:8
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272


The Model I am loading from is an Entity Framework model. In particular the Stories object is a "main" table with quite a few foreign key relationships.  Here is the view code.

@using System.Collections
@model IEnumerable<MWestRazor.Models.UserStory>
@{
    ViewBag.Title = "Stories";
}
<h2>
    USer Stories</h2>
@(
 Html.Telerik().Grid(Model).Name("ContactGrid").DataKeys(datakey => datakey.Add(c => c.StoryId).RouteKey("StoryId"))
        .DataBinding(databinding =>
            databinding.Server()
            .Select("UserStories", "Admin")
            .Delete("DeleteStory", "Admin")
            .Update("UpdateStory", "Admin"))
            .Columns(columns =>
                         {
                             columns.ForeignKey(story => story.UserId, (IEnumerable)ViewData["AllUsers"], "UserId", "FirstName").Title("First Name").ReadOnly(true);
                             columns.ForeignKey(story => story.UserId, (IEnumerable)ViewData["AllUsers"], "UserId", "LastName").Title("Last Name").ReadOnly(true);
                             columns.ForeignKey(story => story.UserId, (IEnumerable)ViewData["AllUsers"], "UserId", "Email").Title("Email Address").ReadOnly(true);
                             columns.ForeignKey(story => story.UserId, (IEnumerable)ViewData["AllUsers"], "UserId", "PoatalCode").Title("Postal Code").ReadOnly(true);
                             columns.Bound(column => column.Story).Title("Story").ReadOnly(true);
                             columns.ForeignKey(story => story.Contact, (IEnumerable)ViewData["ContactOptions"], "ContactID", "ContactDescription").Title("Contact");
                             columns.ForeignKey(story => story.Status, (IEnumerable)ViewData["StatusOptions"], "StatusId", "StatusDescription").Title("Status");
                             columns.ForeignKey(story => story.Category, (IEnumerable)ViewData["CategoryOptions"], "CategoryId", "CategoryDescription").Title("Category");
                             columns.Command(commands =>
                                                 {
                                                     commands.Edit().ButtonType(GridButtonType.Image);
                                                     commands.Delete().ButtonType(GridButtonType.Image);
                                                 }).Title("Editing");
 
                         })
                              .Editable(editing => editing.Mode(GridEditMode.PopUp))
            .Sortable()
            .Pageable()
 
      )


Please help. I would really rather use the ForeignKeyColumn type then creating a custom model and modifying things that way....

3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 27 Jan 2012, 03:55 PM
Hello Chris,

Foreign key columns are used to edit a foreign key property using the corresponding display text from the collection you have passed.
For example: You have a Person which has property called CountryID. When you create a foreign key column you can choose the country name instead of dealing with IDs. From the code you shared you are trying to modify the UserID property on multiple foreign key columns which is not supported.
Also you should use different edit mode than Popup/InForm if you want to use of ForeignKey column.

Regards,
Petur Subev
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
0
Chris McNear
Top achievements
Rank 1
answered on 28 Jan 2012, 11:23 PM
Turns out the main issue was the Pop up editor.  I wasn't actually trying to edit any of the User fields (they are readonly )

Thanks for the tip about the edit mode. 

Although I would suggest that be entered as a Bug whether for extensions or manual is up to you. But nothing in the documentation says Pop Up edit mode can't be used for Foreign Key Columns, so it should either work or the documentation should be updated accordingly.

Thanks again.
0
Petur Subev
Telerik team
answered on 31 Jan 2012, 04:05 PM
Hello Chris,

I am sorry I misled you in my previous reply. The error you reported was because you declared multiple ForeignKey columns bound to a single field. What I was trying to explain is that if you are using Popup/Inform edit mode you need to add an [UIHint("GridForeignKey")] attribute to your model property (UserId in your case) in order to make your Popup view use this editor template. If you are using InLine or Batch editing this step is not required.
Once again I apologize for the misleading reply

Kind regards,
Petur Subev
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
Chris McNear
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Chris McNear
Top achievements
Rank 1
Share this question
or