I have a grid (EditPersonnelRolesGrid) with a column holding a dropdownlist for a training role. For each row on this list the dropdownlist will hold different values.
I use an EditorTemplate (GridPersonnelRoles) to render the dropdownlist. The dropdown list has a Datasource.Read() where it passes the training exercise id to the controller which then passes back the relevant select list of training roles. The exercise id is stored as a column on EditPersonnelRolesGrid. A JavaScript function (getParentId) tries to get this information from the grid row. This code was adapted from other threads regarding the same problem. However this javascript function always returns the first row of grid values regardless of what row is selected. I would appreciate any help.
EditPersonnelRoles.cshtml:
@model IEnumerable<PersonnelRoleEditorModel>
@using Mallon.IReport.Models
@Html.Hidden(
"ID"
, (object)ViewBag.DrillReport)
@Html.Hidden(
"exID"
, (object)ViewBag.ExerciseID)
@Html.Hidden(
"TrainingEventID"
, (object)ViewBag.TrainingEventID)
@Html.Hidden(
"personID"
, (object)ViewBag.PersonID)
<table style=
"width:100%"
>
<tr>
<td>
<div class=
"list rounded"
>
@(Html.Kendo().Grid<PersonnelRoleEditorModel>()
.Name(
"EditPersonnelRolesGrid"
)
.Editable(editable => editable.Mode(GridEditMode.InLine).Enabled(
true
))
.AutoBind(
true
)
.Columns(columns =>
{
columns.Bound(r => r.PersonnelId).Hidden();
columns.Bound(r => r.ExerciseAllocationId).Hidden();
columns.Bound(r => r.ExerciseName).Title(
"Exercise"
);
columns.ForeignKey(r => r.RoleOid, (System.Collections.IEnumerable)ViewBag.Roles,
"Value"
,
"Text"
)
.EditorTemplateName(
"GridPersonnelRoles"
)
.Title(
"Exercise"
)
.Width(200);
columns.Command(command => { command.Edit(); }).Width(172);
})
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(p => p.PersonnelId);
model.Field(p => p.ExerciseName).Editable(
false
);
})
.Events(e =>
{
e.Error(
"handleAjaxError"
);
})
.Update(update => update.Action(
"UpdateExerciseRoles"
,
"DrillAttendance"
))
.Read(read => read.Action(
"ReadPersonnelRoles"
,
"DrillAttendance"
).Data(
"getPersonnelRolesValues"
))
))
</div>
</td>
</tr>
</table>
GridPersonnelRoles.cshtml:
@model object
@(Html.Kendo().DropDownListFor(m => m)
.DataValueField(
"Value"
)
.DataTextField(
"Text"
)
.DataSource(ds => ds.Read(read => read.Action(
"GetDropDownRolesList"
,
"DrillAttendance"
).Data(
"getParentId"
)))
)
Javascript:
function
getParentId() {
var
target = $(event.currentTarget);
var
row = $(target).closest(
"tr"
);
var
grid = $(target).closest(
"[data-role=grid]"
).data(
"kendoGrid"
);
var
dataItem = grid.dataItem(row);
return
{ exerciseId: dataItem.ExerciseAllocationId };
}
Controller code:
public JsonResult GetDropDownRolesList(string exerciseId)
{
ExerciseAllocation ea = DbSession.Get<ExerciseAllocation>(
new
Guid(exerciseId));
SelectList Roles = SelectListHelper.TrainingEventRoles(DbSession, ea.Exercise, ea.Role);
return
Json(Roles, JsonRequestBehavior.AllowGet);
}