This is a migrated thread and some comments may be shown as answers.

Dynamic parameter on read

5 Answers 2059 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 28 Mar 2015, 08:35 PM
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

5 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 01 Apr 2015, 07:18 AM
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.

 
0
Greg
Top achievements
Rank 1
answered on 01 Apr 2015, 01:10 PM
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
0
Petur Subev
Telerik team
answered on 03 Apr 2015, 12:09 PM
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.

 
0
Greg
Top achievements
Rank 1
answered on 04 Apr 2015, 08:21 PM

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

0
Petur Subev
Telerik team
answered on 07 Apr 2015, 10:20 AM
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.

 
Tags
DropDownList
Asked by
Greg
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Greg
Top achievements
Rank 1
Share this question
or