Dynamic parameter on read

6 posts, 0 answers
  1. Greg
    Greg avatar
    24 posts
    Member since:
    Dec 2014

    Posted 28 Mar 2015 Link to this post

    I have a dropdown editor like so:

    model CCProMVC.Models.DivisionModelView

    @(Html.Kendo().DropDownList()
        .Name("DivisionDDL")
        .DataTextField("DivisionName")
            .DataValueField("DivisionID")
        .DataSource(d => d
        .Read(r => r.Action("GetDivisions", "Client",<Need to pass Parent ID here from selected grid>))
        )
    )

    It's used in a Child grid. I need to pass the parent grids ID on the dropdowns read.

    Reason why (I'll try and keep it short) I have a Client (grid) that can have Divisions.  The client can also have Stores that are part of a Client-Division.  I want to display in the Store grid's (child)  Division dropdown only those divisions for the given Client.

    Any suggestions would be helpful.

    Thanks
  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 01 Apr 2015 Link to this post

    Hello Greg,

    You can  define a JavaScript function and pass it to the Data() method of the Transport.Read configuration. Check the second example here:

    http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/faq#how-do-i-send-values-to-my-action-method-when-binding-the-grid

    Now the second part of the question is how to get reference to the parent related model. How exactly are your Grids configured, do you use MVC wrappers?

    If you do then you can use a template expressions like this:

    <script id="template" type="text/kendo-tmpl">
        @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
                .Name("grid_#=EmployeeID#")
                .Columns(columns =>
                {
                    columns.Bound(o => o.OrderID).Width(110);
                    columns.Bound(o => o.ShipCountry).Width(110);
                    columns.Bound(o => o.ShipAddress);
                    columns.Bound(o => o.ShipName).Width(200);
                })
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .PageSize(10)
                            .Read(read => read.Action("HierarchyBinding_Orders", "Grid").Data("function () {return { foo: '#=Country#'}}"))
                )
                .Pageable()
                .Sortable()
                .ToClientTemplate()
        )
    </script>

    Let us know your findings.

    Kind Regards,
    Petur Subev
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Greg
    Greg avatar
    24 posts
    Member since:
    Dec 2014

    Posted 01 Apr 2015 Link to this post

    I did a poor job of explaining the problem.  I have two Telerik Kendo MVC grids (Client (Parent) and Store(Child)).  When I click on the parent it properly displays the stores for the selected Client.  The child or Store grid has a field called division which a Client can have zero or more Divisions.  I need the division column (which is a dropdownlist to show only the divisions for the given Client).  I have attached some of the code.

    I was thinking I can populate the division on databind of the Child grid.  I get references from the Parent via the Change event of the Parent.  The child has .AutoBind(false) so when the user selects the Client (parent) it displays the Stores.
    function ClientGrid_OnRowSelect(arg) {
    var id = GetSelectedClientID();

    $("#ClientDivisionGrid").data("kendoGrid").dataSource.read({ clientId: id });  <=== I happen to be also show the Division grid as well.  That what this is.
        $("#ClientStoreGrid").data("kendoGrid").dataSource.read({ clientId: id });
    $("#DetailContainer").css("display", "block");
    };

    I will see if I can get the template expression working and let you know.  Thanks!

    Greg
  5. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 03 Apr 2015 Link to this post

    Hello Greg,

    Just specify custom editor for the Division field like explained here and put a DropDownlist inside the editor template. The DropDownList and more specifically its dataSource can be configured just like the link that I provided the Read option has a Data() method so you can send the dataItem of the parent Grid.

    .DataSource(dataSource => dataSource.Ajax()
        .Read(read => read
            .Action("Read", "Home")
            .Data("additionalData")
        )
    )
    // -- removed for brevity
    <script>
        function additionalData() {
            return {
                        clientId: id
            };
        }
    </script>

    Let me know if I misunderstood something.

    Kind Regards,
    Petur Subev
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  6. Greg
    Greg avatar
    24 posts
    Member since:
    Dec 2014

    Posted 04 Apr 2015 in reply to Petur Subev Link to this post

    I've made the changes but when I try and Create a new Store it fails with "JavaScript error: " 'Division' is undefined"  in the kendo anonymous(data) function.  Strange thing data does not have the Division property so I see why it errors.  Even stranger is it does not error when editing an existing entry.  That works just fine.

    Has to be something simple I'm missing.

     

    Greg

  7. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 07 Apr 2015 Link to this post

    Hello Greg,

    I am not sure what exactly fails, it sounds like a template fails because the context does not have property 'Division', but I cannot give concrete reason by just inspecting the code. We will need to reproduce the issue in order to say what exactly is the reason.

    Kind Regards,
    Petur Subev
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top
UI for ASP.NET MVC is VS 2017 Ready