SomeRandomDeveloper SomeRandomDeveloper - 1 year ago 159
Javascript Question

Telerik MVC hierarchy grid, editor popup, get parent data in JavaScript event

I've been doing a lot of searching, and can't seem to find any examples that work.

I will simplify the code here as i have a very large telerik mvc hierarchy grid. Here's a child template a few levels down, i need parent information for the previous level.

<script id="leagueTemplate" type="text/kendo-tmpl">
@(Html.Kendo().Grid<LeagueViewModel>()
.Name("grid_#=LeagueTypeId#")
.ToolBar(toolbar =>
{
toolbar.Create().Text("Add New League(Window)");
})
.Events(e => e.Edit("leagueEdit")) // this function runs
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(p => p.LeagueID))
.Read(read => read.Action("Bound_League_Read", "Configuration", new { _leagueTypeId = "#=LeagueTypeId#" }))
.Create(create => create.Action("League_Create", "Configuration").Data("getHeirarchyData")) // this function doesnt run
)
)
</script>

function getHeirarchyData() {
console.log("get heirachy data"); // never runs
}

function leagueEdit(e) {

// this runs
// not sure how to get parent data from e

}

Answer Source

There might be a better way to solve this, and if so i'll check your answer but maybe this will help someone in the meantime.

I solved this by setting default values for the model which works excellent except only for string values because of the way i have to access the values as template syntax strings. So i just added string value equivalents for my hierarchy fields to the model.

            .Model(model =>
              {
                model.Id(p => p.LeagueID);
                model.Field(l => l.strSportId).DefaultValue("#=SportId#");
                model.Field(l => 
                l.strLeagueTypeId).DefaultValue("#=LeagueTypeId#");
              }
            )

Then when i called the edit function, i could get the values like this:

function leagueEdit(e) {
     var sportId = parseInt(e.model.strSportId);
     var leagueTypeId = parseInt(e.model.strLeagueTypeId);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download