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

Grid ForeignKey In-Line Editing not working

10 Answers 344 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ruairi
Top achievements
Rank 1
Ruairi asked on 16 Apr 2015, 11:17 AM

I just updated to release '2015.1.318' and a Grid that was working fine with several ForeignKey columns bound to Guid and selectlists is now not working. I add and update In-Line on the grid. Now when I CREATE a new Entity inline - the editor displays all the data perfectly but the data sent to the server does not include the values selected for the ForeignKey columns. These are sent as Empty Guids i.e.00000000-0000-0000-0000-000000000000.

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

The code for the Grid is below and the EditorTemplate for the ForeignKey column is also there.

 

Grid

<script type="text/javascript">
  function gridEntityAssignments_CreateData() {
      return {
        assignedTo: "@(assignedTo)",
        assignedToOid: "@(assignedToOid)",
      };
  }
</script>
@*All Required*@
@Html.Hidden("IsAssignments", isAssignments)
@Html.Hidden("AssignmentsAssignedToOid", assignedToOid)
@Html.Hidden("AssignmentsAssignedTo", assignedTo)
 
<div style="width: 100%">
  @(Html.Kendo().Grid<Mallon.AssignedTo.Models.ViewGridAssignment>()
  .Name("GridEntityAssignments")
  .AutoBind(isAssignments) //if false - Report not loaded on page initialise
  .HtmlAttributes(new { @class = "ignore" })
  .ToolBar(toolbar =>
  {
    toolbar.Create().HtmlAttributes(new { @class = "dfw-ignoreDirtyLink" });
  })
  .Editable(editable => editable.Mode(GridEditMode.InLine))
  .Scrollable(s => s.Height("auto"))
  .Columns(columns =>
  {
    columns.Bound(p => p.Oid).Visible(false);
    columns.ForeignKey(p => p.UserOid, (System.Collections.IEnumerable)ViewData["AssignmentUsers"], "Value", "Text").EditorTemplateName("GridForeignKeyWithSelect").Title("User");
    columns.ForeignKey(p => p.AssignmentTypeOid, (System.Collections.IEnumerable)ViewData["AssignmentTypes"], "Value", "Text").EditorTemplateName("GridForeignKeyWithSelect").Title("Type");
    columns.Bound(p => p.Info).EditorTemplateName("GridTextArea");
    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
  })
  .DataSource(dataSource => dataSource
    .Ajax()
    .AutoSync(false)
    .Model(model =>
    {
      model.Id(p => p.Oid);
      model.Field(p => p.Oid).DefaultValue(Guid.Empty);
    })
    .Events(e => e.Error("handleAjaxError"))
    .Destroy(update => update.Action("Assignments_GridDelete", "Assignment"))
    .Update(update => update.Action("Assignments_GridUpdate", "Assignment"))
    .Create(update => update.Action("Assignments_GridCreate", "Assignment").Data("gridEntityAssignments_CreateData"))
    .Read(read => read.Action("Assignments_GridRead", "Assignment", new { assignedToOid = assignedToOid }))
  )
)
 
</div>

 

 Editor Template

 

@model object
 
@(
 Html.Kendo().DropDownListFor(m => m)
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"]).OptionLabel("Please Select")
)

 

 

 

 

 

 

 

 

 

 

 

10 Answers, 1 is accepted

Sort by
0
Ruairi
Top achievements
Rank 1
answered on 16 Apr 2015, 04:15 PM

I believe the problem is that the dropdownlists associated with the ForeignKey columns are not firing their select event when I select my selection. If I select an item then select another different item then the correctly selected data gets sent.

Any idea how to fix this?

0
Ruairi
Top achievements
Rank 1
answered on 17 Apr 2015, 09:08 AM

Alright this functionality is broken. The first select event on the ForeignKey dropdownlist is not updating the model values. I have to explicitly set dropDownList values using the onSave event as shown below. Telerik why dosnt this work?

Heres the updates code with the Save event.

  @(Html.Kendo().Grid<Mallon.AssignedTo.Models.ViewGridAssignment>()
  .Name("GridEntityAssignments")
  .AutoBind(isAssignments) //if false - Report not loaded on page initialise
  .HtmlAttributes(new { @class = "ignore" })
  //Save Event is required because Inline eding with DropDownLists (ForegnKeys) dosnt seem to pick up the first select
  //event. So even when I change\select the value the selected value is not populating the model with the data. This
  //is only a problem on the first select.
  .Events(events => events.Edit("onEdit_GridEntityAssignments").Save("onSave_GridEntityAssignments"))
  .ToolBar(toolbar =>
  {
    toolbar.Create().HtmlAttributes(new { @class = "dfw-ignoreDirtyLink" });
  })
  .Editable(editable => editable.Mode(GridEditMode.InLine))
  .Scrollable(s => s.Height("auto"))
  .Columns(columns =>
  {
    columns.Bound(p => p.Oid).Visible(false);
    columns.ForeignKey(p => p.UserOid, (System.Collections.IEnumerable)ViewData["AssignmentUsers"], "Value", "Text").EditorTemplateName("GridForeignKeyWithSelect").Title("User");
    columns.ForeignKey(p => p.AssignmentTypeOid, (System.Collections.IEnumerable)ViewData["AssignmentTypes"], "Value", "Text").EditorTemplateName("GridForeignKeyWithSelect").Title("Type");
    columns.Bound(p => p.Info).EditorTemplateName("GridTextArea");
    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
  })
  .DataSource(dataSource => dataSource
    .Ajax()
    .AutoSync(false)
    .Model(model =>
    {
      model.Id(p => p.Oid);
      model.Field(p => p.Oid).DefaultValue(Guid.Empty);
    })
    .Events(e => e.Error("handleAjaxError"))
    .Destroy(update => update.Action("Assignments_GridDelete", "Assignment"))
    .Update(update => update.Action("Assignments_GridUpdate", "Assignment"))
    .Create(update => update.Action("Assignments_GridCreate", "Assignment").Data("gridEntityAssignments_CreateData"))
    .Read(read => read.Action("Assignments_GridRead", "Assignment", new { assignedToOid = assignedToOid }))
  )
)

Javascript events:

 

//Used to set the initial value for ForeignKey dropDownLists
function onEdit_GridEntityAssignments(e) {
  if (e.model.isNew()) {
    $("#GridEntityAssignments tbody [data-role=dropdownlist]").each(function () {
      var ddl = $(this).data("kendoDropDownList");
      if (ddl) {
        ddl.select(0);
      }
    })
  }
}
 
//This is required because Inline eding with DropDownLists (ForegnKeys) dosnt seem to pick up the first select
//event. So even when I change\select the value the selected value is not populating the model with the data. This
//is only a problem on the first select.
function onSave_GridEntityAssignments(e) {
  $("#GridEntityAssignments 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);
    }
  })
}

 

 

 

 

 

 

 

 

0
Accepted
Boyan Dimitrov
Telerik team
answered on 20 Apr 2015, 07:04 AM

Hello Tyler,

We have fixed this issue and it is already included in our service pack release (2015.1.408). 

I would like to apologize for any inconveniences caused. 

Regards,

Boyan Dimitrov

Telerik

 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Terry
Top achievements
Rank 1
answered on 02 Sep 2016, 08:08 PM
Is there any chance this problem has been reintroduced?  I'm seeing the exact same results.  On an insert, the foreign key field isn't getting updated.  I went into the database and set a value - and then the update worked.  But update doesn't work if the value is initially null.  I am on version 2016.2.714.
0
Boyan Dimitrov
Telerik team
answered on 06 Sep 2016, 08:42 AM

Hello Terry,

Could you please try to set valuePrimitive option to true for the DropDownList editor of the foreign key column and let us know how that worked? 

Regards,
Boyan Dimitrov
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Terry
Top achievements
Rank 1
answered on 06 Sep 2016, 12:15 PM
Sorry - I can't figure out how to set that option in a Razor view.  Here's how I'm defining the dropdownlist.  If you can show me how to add that option, then I'll try it.  Thanks.
columns.ForeignKey(c => c.GenerationTypeID, (System.Collections.IEnumerable)@ViewBag.gentypes, "Value", "Text").Title("Generation");
0
Konstantin Dikov
Telerik team
answered on 08 Sep 2016, 04:56 AM
Hello Terry,

The property should be set to the editor template and not in the column definition. You should have a GridForeignKey editor template within your Views/Shared/EditorTemplates folder with the DropDownList:
@(
 Html.Kendo().DropDownListFor(m => m)       
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
        .ValuePrimitive(true)
)

Hope this helps.


Regards,
Konstantin Dikov
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Terry
Top achievements
Rank 1
answered on 08 Sep 2016, 03:31 PM

Konstantin - that worked.  Thanks.

For anyone else reading this.  I didn't make any changes to my view.  What Konstantin showed me to change was the GridForeignKey Editor Template. 

0
Priyanka
Top achievements
Rank 1
answered on 11 Sep 2017, 02:54 PM

For me it is still not working

 

   @(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"))
                    )

 

please help.

0
Konstantin Dikov
Telerik team
answered on 13 Sep 2017, 10:50 AM
Hi Priyanka,

Could you please elaborate if you have included the GridForeignKey.cshtml template in the Views/Shared/EditorTemplates folder as suggested in my previous post?

If the template is available and if you are populating correctly the data collection that you are passing to the ForeignKey column, everything should be working correctly.


Regards,
Konstantin Dikov
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
Ruairi
Top achievements
Rank 1
Answers by
Ruairi
Top achievements
Rank 1
Boyan Dimitrov
Telerik team
Terry
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Priyanka
Top achievements
Rank 1
Share this question
or