Foreignkey column sends incorrect data to server with creating new row

8 posts, 0 answers
  1. Vladimir
    Vladimir avatar
    15 posts
    Member since:
    Jan 2011

    Posted 21 Dec 2012 Link to this post

    Grid in inline edit mode.

    @(Html.Kendo().Grid<Security.UserViewModel>()
        .Name("Users")
        .Columns(columns =>
        {
            columns.Bound(c => c.UserId).Hidden();
            columns.Bound(c => c.UserName).Title("Логин");
            columns.Bound(c => c.FirstName).Title("Имя");
            columns.Bound(c => c.LastName).Title("Фамилия");
            columns.Bound(c => c.Email).Title("E-mail");
            columns.ForeignKey(c => c.FirstRoleId, (System.Collections.IEnumerable)ViewData["roles"], "RoleID", "RoleName").Title("Роль");
            columns.Command(command =>
                {
                    command.Edit();
                    command.Destroy();
                });
        })
        .ToolBar(toolBar => toolBar.Create())
        .Editable(editing => editing.Mode(GridEditMode.InLine))
        .Pageable()
        .Sortable()
        .Scrollable()
         .DataSource(dataSource => dataSource       
            .Ajax()                
            .Events(events => events.Error("error"))
            .Model(model => {
                model.Id(u => u.UserId);
            })
            .Create(update => update.Action("Create", "Users"))
            .Read(read => read.Action("Read", "Users"))
            .Update(update => update.Action("Update", "Users"))
            .Destroy(delete => delete.Action("Delete", "Users"))
        )
    )

    When I add new row grid sends to the server blank "FirstRoleName" instead of "FirstRoleId". When I update the row all works fine.
    Made temporary workaround:

    $(function () {
        var grid = $("#Users").data("kendoGrid");
        // bind to the save event
        grid.bind("save", function (e) {
            if (e.model.isNew()) {
                e.model.FirstRoleId = $("input#FirstRoleId").val();
            }
        });
    });

    I think it is a bug.

  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 25 Dec 2012 Link to this post

    Hello Vladimir,

    I am not able to reproduce such behavior on my side, could you send us sample project so we can take a look? 

    Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Chris
    Chris avatar
    27 posts
    Member since:
    May 2010

    Posted 17 Jan 2013 Link to this post

    I'm experiencing the same problem.  I think it has to do with that exact foreign key function.  Mine looks like:
    columns.ForeignKey(r => r.ResourceTypeId, ViewBag.ResourceTypes as IList<SelectListItem>, "Value", "Text");
    It appears that it tries to use the property that was assigned to "Text" of the SelectListItem to do the post.
    IResourceTypeService resourceTypeService = Services.CreateService<IResourceTypeService>();
                ListModel<ResourceType> resourceTypes = new ListModel<ResourceType>();
     
                resourceTypes.Items = resourceTypeService.LoadAll();
                resourceTypes.Value = r => r.ResourceTypeId;
                resourceTypes.Text = r => r.ResourceTypeText;
     
                return resourceTypes.SelectItems;
    So, in my case it tries to post "ResourceTypeText" instead of "ResourceTypeId".
  4. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 21 Jan 2013 Link to this post

    Hello Chris,

    Could you tell us how to reproduce this or send us project which we can run?

    Thank you in advance for the understanding.

    Kind Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Chris
    Chris avatar
    27 posts
    Member since:
    May 2010

    Posted 22 Jan 2013 Link to this post

    Hope this helps.  Looks like it happens on the Insert, the Update appears to work.  ResourceTypeId is the field that doesn't seem to get posted.
  6. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 24 Jan 2013 Link to this post

    Hello Vladimir,

    Thank you for sending the sample project. The problem occurs because you have not set default value for the ForeignKey field.

    You can set default value like this:

    .Model(model => { model.Id(r => r.ResourceId);
                   model.Field(r => r.ResourceTypeId).DefaultValue(0);
               })


    Kind Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  7. Chris
    Chris avatar
    27 posts
    Member since:
    May 2010

    Posted 24 Jan 2013 Link to this post

    Interesting that the entire control doesn't work if you don't set that.  Maybe that could be patched in a future release to assume a default value of 0 or an index of 0.  Thanks for the reply and fix.
  8. Archana
    Archana avatar
    5 posts
    Member since:
    Aug 2013

    Posted 23 Sep 2014 Link to this post

    Please include this in documentation.
Back to Top