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>
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With