Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telerik Kendo MVC Grid child template get default value from parent

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>
like image 324
richardpotter.au Avatar asked Jun 04 '14 19:06

richardpotter.au


1 Answers

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);
}
like image 96
James Avatar answered Oct 21 '22 14:10

James