How to transfer a Kendo grid line model to an editable template

I have a Kendo grid that has a pop-up editable template. If possible, I would like to pass the model (the row model, or at least its identifier) ​​to the editable template

Grid

@(Html.Kendo().Grid<Client>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(c => c.Name).Width(140);
        columns.Bound(c => c.Status);
        columns.Bound(c => c.ProcesingStyle);
        columns.Bound(c => c.ArchiveDays);
        columns.Command(command =>
        {
            command.Edit().Text(" ");
            command.Destroy().Text(" "); ;
        }).Width(90);

    })
     .ToolBar(toolbar => toolbar.Create().Text("New"))
     .Editable(editable => editable
        .Mode(GridEditMode.PopUp)
        .TemplateName("Client").AdditionalViewData(new { Client = Model })
        .Window(w => w.Title("Site")))
    .HtmlAttributes(new { style = "height: 380px;" })
    .Scrollable()
    .Sortable()
    .Selectable()
    .Resizable(resize => resize.Columns(true))
    .Reorderable(reorder => reorder.Columns(true))
    .Events(events => events.Change("onChange"))
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Get", "Clients"))
        .Model(model => model.Id(p => p.Id))
                .Create(update => update.Action("Create", "Clients"))
                .Update(update => update.Action("Update", "Clients"))
                .Destroy(update => update.Action("Destroy", "Clients"))
    )
)

Template

@model Client
@(Html.Kendo().ComboBoxFor(m => m.Plan)
    .DataTextField("Name")
    .DataValueField("Id")
    .Placeholder("Select Plan...")
    .HtmlAttributes(new { style = "width:300px" })
    .Filter(FilterType.Contains)
    .MinLength(3)
    .DataSource(source => 
        source.Read(read => 
            read.Action("GetPlans", "Plans",new {ClientId = Model.Id}))))

Everything works fine, except that I need to use the Id of the string / model inside the template, in particular, I need to pass model.Id (which is the identifier of the string model) to the Combobox action in the template, so I can filter the data correctly.

This is a line break in the grid,

.TemplateName("Client").AdditionalViewData(new { Client = Model })

As a result, the model inside the template is always null, im not sure how to pass the data that I need for the template

Anyway, can I do this, or should I look at a different approach?

+4
1

, , - JavaScript , .

function getClientId() {
    var row = $(event.srcElement).closest("tr");
    var grid = $(event.srcElement).closest("[data-role=grid]").data("kendoGrid");
    var dataItem = grid.dataItem(row);  
    if (dataItem)
        return { clientId: dataItem.Id }
    else
        return { clientId: null }
    }

.DataSource(source => source.Read(read => read.Action("GetPlans", "Plans").Data("getClientId"))))

. , javascript EditorTemplate, .

- , .

+3

Source: https://habr.com/ru/post/1537590/


All Articles