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

Foreignkey column sends incorrect data to server with creating new row

7 Answers 180 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vladimir
Top achievements
Rank 2
Vladimir asked on 21 Dec 2012, 02:30 PM

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.

7 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 25 Dec 2012, 10:03 AM
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!
0
Chris
Top achievements
Rank 1
answered on 17 Jan 2013, 05:04 PM
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".
0
Petur Subev
Telerik team
answered on 21 Jan 2013, 12:14 PM
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!
0
Chris
Top achievements
Rank 1
answered on 22 Jan 2013, 01:02 PM
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.
0
Petur Subev
Telerik team
answered on 24 Jan 2013, 11:42 AM
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!
0
Chris
Top achievements
Rank 1
answered on 24 Jan 2013, 01:48 PM
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.
0
Archana
Top achievements
Rank 1
answered on 23 Sep 2014, 05:53 PM
Please include this in documentation.
Tags
Grid
Asked by
Vladimir
Top achievements
Rank 2
Answers by
Petur Subev
Telerik team
Chris
Top achievements
Rank 1
Archana
Top achievements
Rank 1
Share this question
or