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

grid-foreignkey-in-line-editing-not-working

1 Answer 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Priyanka
Top achievements
Rank 1
Priyanka asked on 08 Sep 2017, 07:31 PM

when I UPDATE an existing entity it all works fine, the problem is only when I CREATE\INSERT.

 

Here is the code below please help

 

        <div>
                @*Phone Carrier*@
                @(Html.Kendo().Grid<OSH.Domain.Models.PhoneCarrier.AppPhoneCarrierItem>()
                .Name("CarrierGrid")
                .Columns(columns =>
                {
                    columns.Bound(e => e.PhoneCarrierId).Width(350).Hidden();
                    columns.Bound(e => e.PhoneCarrier).Width(350);
                    columns.Command(command => { command.Edit().Text(" "); command.Destroy().Text(" "); });
                })
                .ToolBar(toolbar => { toolbar.Create().Text("Add New Carrier"); })
                .Editable(editable => editable.Mode(GridEditMode.InLine))
                .Sortable()
                .Pageable()
                .Scrollable()
                .ColumnMenu()
                .Filterable()
                .HtmlAttributes(new { style = "height:420px;" })
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .PageSize(10)
                    .Model(model => model.Id(p => p.PhoneCarrierId))
                    .Read(read => read.Action("readCarrier", "Admin"))
                    .Create(update => update.Action("CreateCarrier", "Admin"))
                    .Update(update => update.Action("EditCarrier", "Admin"))
                    .Destroy(update => update.Action("DeleteCarrier", "Admin"))
                    .Events(events => events.Error("onError3").Sync("sync_handler"))
                    )
                .Pageable(x => x.PageSizes(new List<object> { 5, 10, 15, 20, "all" }).Refresh(true))
                .Resizable(resize => resize.Columns(true))
                .Reorderable(reorder => reorder.Columns(true))
                )
            </div>

 <div>
                @*Phone Number*@
                @(Html.Kendo().Grid<OSH.Domain.Models.PhoneNumber.AppPhoneNumberItem>()
                .Name("PhoneNumberGrid")
                .Columns(columns =>
                {
                    columns.Bound(e => e.PhoneNumber);
                    columns.Bound(e => e.Description);
                    //columns.Bound(e => e.PhoneNumberTypeId);
                    //columns.Bound(e => e.PhoneCarrierId);
                    columns.ForeignKey(e => e.PhoneNumberTypeId, (System.Collections.IEnumerable)ViewData["PNType"], "PhoneNumberTypeId", "PhoneNumberType")
                   .Title("Phone Number Type");
                    columns.ForeignKey(e => e.PhoneCarrierId, (System.Collections.IEnumerable)ViewData["PhoneCarrier"], "PhoneCarrierId", "PhoneCarrier")
               .Title("Phone Carrier");
                    columns.ForeignKey(e => e.LocationId, (System.Collections.IEnumerable)ViewData["Location"], "LocationId", "LocationName")
               .Title("Location");
                    //columns.Bound(e => e.LocationId);
                    columns.Command(command => { command.Edit().Text(" "); command.Destroy().Text(" "); });
                })
                .ToolBar(toolbar => { toolbar.Create(); })
                .Editable(editable => editable.Mode(GridEditMode.InLine))
                .Sortable()
                .Pageable()
                .Scrollable()
                .ColumnMenu()
                .Filterable()
                .HtmlAttributes(new { style = "height:420px;", data_value_primitive = "true" })
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .PageSize(10)
                    .Model(model => model.Id(p => p.PhoneCarrierId))
                    .Read(read => read.Action("readPhoneNumber", "Admin"))
                    .Create(update => update.Action("CreatePhoneNumber", "Admin"))
                    .Update(update => update.Action("EditPhoneNumber", "Admin"))
                    .Destroy(update => update.Action("DeletePhoneNumber", "Admin"))
                    .Events(events => events.Error("error_handler").Sync("sync_handler"))
                    )
                .Pageable(x => x.PageSizes(new List<object> { 5, 10, 15, 20, "all" }).Refresh(true))
                .Events(e => e.Edit("onPhoneNumberGridEdit").Save("onSavingPhoneNumber"))
                .Resizable(resize => resize.Columns(true))
                .Reorderable(reorder => reorder.Columns(true))
                )
            </div>
        </div>

 

  function onSelect(e) {
                var x = e.item;
                var tabSelectedIndex = $(x).index();
                if (tabSelectedIndex == 2) {
                    $.ajax({
                        url: '@Url.Action("UpdateNewDataType", "Admin")',
                        cache: false,
                        type: 'GET',
                        success: function (Newdata) {
                            var data = [];
                            for (var i = 0; i < Newdata.length; i++) {
                                data.push({ text: Newdata[i].PhoneNumberType, value: Newdata[i].PhoneNumberTypeId });
                            }
                            var grid = $("#PhoneNumberGrid").data("kendoGrid");
                            grid.columns.filter(function (item) {
                                return item.field === "PhoneNumberTypeId";
                            }).forEach(function (item) {
                                item.values = data;
                            });
                            grid.setOptions({ columns: grid.columns, toolbar: [{ name: "create", text: "Add Phone Number" }] });
                            grid.refresh();
                        }
                    });


                    $.ajax({
                        url: '@Url.Action("UpdateNewDataCarrier", "Admin")',
                        cache: false,
                        type: 'GET',
                        success: function (Newdata) {
                            var data = [];
                            for (var i = 0; i < Newdata.length; i++) {
                                data.push({ text: Newdata[i].PhoneCarrier, value: Newdata[i].PhoneCarrierId });
                            }
                            var grid = $("#PhoneNumberGrid").data("kendoGrid");
                            grid.columns.filter(function (item) {
                                return item.field === "PhoneCarrierId";
                            }).forEach(function (item) {
                                item.values = data;
                            });
                            grid.setOptions({ columns: grid.columns, toolbar: [{ name: "create", text: "Add Phone Number" }] });
                            grid.refresh();
                        }
                    });
                }

            }

 

function onSavingPhoneNumber(e) {
            $("#PhoneNumberGrid tbody [data-role=dropdownlist]").each(function () {
                var ddl = $(this).data("kendoDropDownList");
                if (ddl) {
                    var v = ddl.value();
                    var p = ddl.list.attr('id').replace('-list', '');
                    if (p) e.model.set(p, v);
                }
            })

----------------------------------------------------------------------------------------------------------------------------

Controller

 

   public ActionResult EditPhoneNumber([DataSourceRequest] DataSourceRequest request, AppPhoneNumberItem AppPhoneNumberItem)
        {
            PhoneNumberUpdateResponse response = new PhoneNumberUpdateResponse();
            if (AppPhoneNumberItem != null && ModelState.IsValid)
            {
                PhoneNumberRequest request2 = new PhoneNumberRequest();
                request2.PhoneNumberId = AppPhoneNumberItem.PhoneNumberId;
                request2.PhoneNumber = AppPhoneNumberItem.PhoneNumber;
                request2.Description = AppPhoneNumberItem.Description;
                request2.PhoneNumberTypeId = AppPhoneNumberItem.PhoneNumberTypeId;
                request2.PhoneCarrierId = AppPhoneNumberItem.PhoneCarrierId;
                request2.LocationId = AppPhoneNumberItem.LocationId;
                response = phoneNumberApi.UpdatePhoneNumber(request2);
                if (response.Error != null)
                {
                    ErrorLogRequest errorReq = new ErrorLogRequest()
                    {
                        LogDate = DateTime.Now,
                        LogMethod = "UpdatePhoneNumber",
                        LogPage = "Admin",
                        LogPerson = User.Identity.Name,
                        LogInfo = response.Error.ErrorCode + " " + response.Error.ErrorMessage + " " + response.Error.ExceptionObject.Message
                    };
                    apiErrorLog.InsertErrorLog(errorReq);
                }
            }
            BindViewBag();
            return Json(new[] { response }.ToDataSourceResult(request, ModelState));
        }

 

public void BindViewBag()
        {

 ViewData["PhoneCarrier"] = PhoneCarrier;
            ContactTypeRequest request4 = new ContactTypeRequest();
            var ContactType = contactTypeApi.GetContactTypeList(request4).data.Select(p => new AppContactTypeItem()
            {
                ContactTypeId = p.ContactTypeItem.ContactTypeId,
                ContactType = p.ContactTypeItem.ContactType
            });

}

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 13 Sep 2017, 07:26 AM
Hello Priyanka,

Thank you for the provided code.

Could you please advise if there is any error in the console or on the server, so we can better understand the issue?

The configuration looks good, the only places that are different from the recommended approach are the on the select event on the BindViewBag method.

I can suggest inspecting the differences from our demo:

http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn

Also, the runnable example with a foreign key column can prove helpful as well:

http://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/configure-foreignkey-columns-combobox-edit-template

If additional assistance is needed, please send a fully runnable example reproducing the issue and we will gladly assist further.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Priyanka
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or