Hierarchy - problem with parent ID

2 posts, 0 answers
  1. Artur
    Artur avatar
    4 posts
    Member since:
    Sep 2014

    Posted 03 Dec 2014 Link to this post

    How can I get the parent ID when I trying to add new subelement? See attachment.

    Sub-grid code template looks like below:
    <script id="template" type="text/kendo-tmpl">
    @(Html.Kendo().Grid<ZakresViewModel>()
    .Name("podzakresy#=IdZakres#")
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .ToolBar(toolbar =>
    {
        toolbar.Create().Text("<span class=\"fa fa-plus\"></span> " + SettingsStrings.DodajNowyPodzakres).HtmlAttributes(new { @class = "btn btn-warning", idZakresNadrzedny = "#=IdZakres#" });
    })
    .Columns(columns =>
    {
        columns.Bound(c => c.Numer).Width(80).Title(SharedStrings.Numer);
        columns.Bound(c => c.Nazwa).Title(SharedStrings.Nazwa);
        columns.Command(command => command.Edit().UpdateText(SharedStrings.Zapisz).CancelText(SharedStrings.Anuluj).Text(SharedStrings.Edytuj)).Width(100);
        columns.Command(command => command.Destroy().Text(SharedStrings.Usun)).Width(100);
    })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .Model(model => { model.Id(p => p.IdZakres); })
        .Read(read => read.Action("GridPodzakresy_Read", "Settings", new { IdZakresNadrzedny = "#=IdZakres#" }))
                            .Create(create => create.Action("GridPodzakresy_PopupCreate", "Settings", new { IdZakresNadrzedny = "#=IdZakres#" }))
                            .Update(update => update.Action("GridZakresy_PopupUpdate", "Settings"))
                            .Destroy(delete => delete.Action("GridZakresy_PopupDestroy", "Settings"))
    )
    .Pageable()
    .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
    .Sortable()
    .ToClientTemplate()
    )
    </script>


    Main grid code looks like below:
    @(Html.Kendo().Grid<ZakresViewModel>()
    .Name("grid-zakresy")
    .HtmlAttributes(new { style = "height:100%; cursor:default" })
    .ClientDetailTemplateId("template")
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .ToolBar(toolbar => toolbar.Create().Text("<span class=\"fa fa-plus\"></span> " + SettingsStrings.DodajNowyZakres).HtmlAttributes(new { @class = "btn btn-primary" }))
    .Columns(columns =>
    {
        columns.Bound(c => c.IdKontrakt).Visible(false).Filterable(false);
        columns.Bound(c => c.IdZakres).Visible(false).Filterable(false);
        columns.Bound(c => c.IdZakresNadrzedny).Visible(false).Filterable(false);
        columns.Bound(c => c.Numer).Width(80).Title(SharedStrings.Numer);
        columns.Bound(c => c.Nazwa).Title(SharedStrings.Nazwa);
        columns.Command(command => command.Edit().UpdateText(SharedStrings.Zapisz).CancelText(SharedStrings.Anuluj).Text(SharedStrings.Edytuj)).Width(100);
        columns.Command(command => command.Destroy().Text(SharedStrings.Usun)).Width(100);
    })
    .Pageable(pageable => pageable.Refresh(true).ButtonCount(3))
    .Sortable(s => { s.AllowUnsort(true); s.SortMode(GridSortMode.MultipleColumn); })
    .Scrollable(scr => { scr.Height("100%"); scr.Enabled(false); })
    .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .Model(model => { model.Id(p => p.IdZakres); })
        .Read(read => read.Action("GridZakresy_Read", "Settings", new { IdKontrakt = Model.IdKontrakt }))
        .Create(create => create.Action("GridZakresy_PopupCreate", "Settings"))
        .Update(update => update.Action("GridZakresy_PopupUpdate", "Settings"))
        .Destroy(delete => delete.Action("GridZakresy_PopupDestroy", "Settings"))
    )
    )

    Action in controller when I trying to create new element. At this action I need parent ID.
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GridPodzakresy_PopupCreate([DataSourceRequest] DataSourceRequest request, ZakresViewModel zakres)
    {
        if (zakres != null && ModelState.IsValid)
        {
            //some code
        }
     
        return Json(new[] { zakres }.ToDataSourceResult(request, ModelState));
    }

    Thanks in advance

  2. Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 05 Dec 2014 Link to this post

    Hi Artur,

    The easiest way will be to pass is as a URL parameter. I noticed that you have already added it to the create action. Then you should change the action method in order to get it mapped as a parameter, something similar to the following:

    public ActionResult GridPodzakresy_PopupCreate([DataSourceRequest] DataSourceRequest request, ZakresViewModel zakres, int IdZakresNadrzedny)
    {
       /*....*/
    }

    Note that if the parameter name matches the property name of the model, it may not be bound correctly. In this case you should change the name of the parameter.

    Regards,
    Rosen
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top