7

Hi I am editing in Kendo MVC Razor child template, I need to set the default value for the item id from the parent. It works if the property I try to set is a string, but not if it is an int. See the comment in the code below. If it is not possible to do this, can someone please suggest a workaround? Thanks.

@using Harpoon.DomainLogic
@using Kendo.Mvc.UI

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@(Html.Kendo().Grid<UserStandardCodeType>().Name("grid")
.DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("GetUserStandardCodesTypes_Ajax", "UserStandardCode")))
.Columns(columns =>
  {
      columns.Bound(usct => usct.InternalCode);
      columns.Bound(usct => usct.PresentationName);
      columns.Bound(usct => usct.Description);
  })
.ClientDetailTemplateId("client-template")
)

<script id="client-template" type="text/x-kendo-template">
    @(Html.Kendo().Grid<UserStandardCode>().Name("grid_#=Id#") // make sure the Name is unique
          .Columns(columns =>
          {
              columns.Bound(usc => usc.InternalCode);
              columns.Bound(usc => usc.PresentationName);
              columns.Bound(usc => usc.Description);
              columns.Bound(usc => usc.IsEnabled);
              columns.Command(commands =>
              {
                  commands.Edit(); 
                  commands.Destroy(); 
              }).Title("Commands").Width(200);
          })
          .DataSource(dataSource => dataSource.Ajax()
              .Read(read => read.Action("GetUserStandardCodes_Ajax", "UserStandardCode", new { CodeTypeId = "#=Id#" }))
              .PageSize(2)
              .Model(model =>
              {
                  model.Field(usc => usc.CodeTypeId).DefaultValue("#=Id#"); // this line fails converting from string to int
                  model.Id(usc => usc.Id); // specify the unique id column
              })
              .Create(create => create.Action("CreateUserStandardCode_Ajax", "UserStandardCode"))
              .Update(update => update.Action("UserStandardCodes_Ajax", "UserStandardCode"))
              .Destroy(destroy => destroy.Action("UserStandardCodes_Ajax", "UserStandardCode"))
          )
          .Pageable()
          .ToolBar(toolbar => toolbar.Create()) 
          .Editable(editable => editable.Mode(GridEditMode.InLine))
          .ToClientTemplate()
          )
</script>

2 Answers2

8

Try adding a JavaScript function to handle the Save event

.Events(e => e.Save("save"))

in order to set the parent ID:

function save(e) {
  var parentGrid = $("#grid").data("kendoGrid");                        
  var parentRow = e.container.closest(".k-detail-row").prev(".k-master-row");                        
  var parentDataItem = parentGrid.dataItem(parentRow);

  e.model.set("CodeTypeId", parentDataItem.Id);
}
James
  • 460
  • 2
  • 8
  • 15
  • 2
    I got it to work by changing the last line to [code]e.model.CodeTypeId = parentDataItem.id; //note id is lower case – Dan Randolph Apr 02 '15 at 23:39
  • This is exactly what I needed to hear. Except that it didn't work for me, I think since I'm using PopUp editing instead of Inline. I had to change `e.container` to `e.sender.table`. Then it worked :) – Nacht Oct 10 '19 at 01:29
0

You can try to use the JavaScript function of parseInt to transform the string to an int. The Kendo UI "DefaultValue" works similarly to the ClientTemplate. There is some extra logic to check for null so you don't get a JavaScript error.

// Use JavaScript parseInt to transform the field from string to int
model.Field(usc => usc.CodeTypeId).DefaultValue("#= Id != null ? parseInt(Id) : 0 #"); 
piercove
  • 811
  • 9
  • 17
  • 3
    Hi piercove thanks for the suggestion however when I tried it I still get the same exception about attempting to convert string to int – richardpotter.au Jun 11 '14 at 05:48