I want to be able to select a row in a parent grid and be able to see (and edit or add) all of it's children. The parent is a lessor and the child is the leased equipment. I created a through away project where I get the expected behavior but when I implement it my real project it is not working. I have verified I am getting a JSON response back from my controller but the grid is not filling in with the data.
Here is my stripped down code from the razor view:
@using Cci.Web.PPDeclaration.Models
@(Html.Kendo().Grid<Cci.Web.PPDeclaration.Domain.DbEntities.Lessor>()
.Name("LeasingGrid")
.Columns(columns =>
{
columns.Bound(l => l.LessorId).Hidden(true);
columns.Bound(l => l.LessorName).Title("Lessor").Width(175);
}
)
.Selectable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("GetLessors", "Leasing"))
.Events(events => events.Error("error_handler"))
)
.Events(events => events.Change("change"))
)
@(Html.Kendo().Grid<Cci.Web.PPDeclaration.Domain.DbEntities.Lease>()
.Name("LeaseGrid")
.Columns(columns =>
{
columns.Bound(m => m.LeaseDetail).Width(250).Title("Property Description");
columns.Bound(m => m.LeaseNumber).Width(80).Title("Lease Number");
})
.AutoBind(false)
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("SelectLease", "Leasing").Data("data"))
.Events(events => events.Error("error_handler"))
)
)
<script type="text/javascript">
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
function change() {
$("#LeaseGrid").data("kendoGrid").dataSource.read();
}
function data() {
var grid = $("#LeasingGrid").data("kendoGrid");
return {
lessorId: grid.dataItem(grid.select()).LessorId
}
}
</script>
@section Javascript
{
<script src="@Url.Content("~/Scripts/kendo/2013.2.716/jquery.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2013.2.716/kendo.all.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2013.2.716/kendo.aspnetmvc.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo.modernizr.custom.js")"></script>
<script src="@Url.Content("~/Scripts/EditableGrids.js")"></script>
}
Here is the controller code:
public class LeasingController : System.Web.Mvc.Controller
{
private ILeasingRepository _repository;
public LeasingController() : this(new LeasingRepository())
{
}
public LeasingController(ILeasingRepository repository)
{
_repository = repository;
}
public ActionResult Index()
{
return View();
}
#region Lessors
public ActionResult GetLessors([DataSourceRequest]DataSourceRequest request)
{
LeasingModel model = new LeasingModel { Lessors = _repository.GetActiveLessors(SessionData.AccountNo) };
return Json(model.Lessors.ToDataSourceResult(request));
}
#endregion
#region leases
public ActionResult GetLeases([DataSourceRequest]DataSourceRequest request)
{
LeasingModel model = new LeasingModel { Leases = _repository.GetActiveLeases(SessionData.AccountNo) };
return Json(model.Leases.ToDataSourceResult(request));
}
public ActionResult SelectLease([DataSourceRequest]DataSourceRequest request, int lessorId)
{
SessionData.LessorId = lessorId;
return Json(new[] { FilteredLeases() }.ToDataSourceResult(request, ModelState),JsonRequestBehavior.AllowGet);
}
private List<Lease> FilteredLeases()
{
return _repository.GetActiveLeases(SessionData.AccountNo).Where(ls => ls.LessorId == SessionData.LessorId).ToList();
}
#endregion
Any help will be appreciated.
Thanks,
Dave Brown
P.S. I've also attached the through away project.
Here is my stripped down code from the razor view:
@using Cci.Web.PPDeclaration.Models
@(Html.Kendo().Grid<Cci.Web.PPDeclaration.Domain.DbEntities.Lessor>()
.Name("LeasingGrid")
.Columns(columns =>
{
columns.Bound(l => l.LessorId).Hidden(true);
columns.Bound(l => l.LessorName).Title("Lessor").Width(175);
}
)
.Selectable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("GetLessors", "Leasing"))
.Events(events => events.Error("error_handler"))
)
.Events(events => events.Change("change"))
)
@(Html.Kendo().Grid<Cci.Web.PPDeclaration.Domain.DbEntities.Lease>()
.Name("LeaseGrid")
.Columns(columns =>
{
columns.Bound(m => m.LeaseDetail).Width(250).Title("Property Description");
columns.Bound(m => m.LeaseNumber).Width(80).Title("Lease Number");
})
.AutoBind(false)
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("SelectLease", "Leasing").Data("data"))
.Events(events => events.Error("error_handler"))
)
)
<script type="text/javascript">
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
function change() {
$("#LeaseGrid").data("kendoGrid").dataSource.read();
}
function data() {
var grid = $("#LeasingGrid").data("kendoGrid");
return {
lessorId: grid.dataItem(grid.select()).LessorId
}
}
</script>
@section Javascript
{
<script src="@Url.Content("~/Scripts/kendo/2013.2.716/jquery.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2013.2.716/kendo.all.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo/2013.2.716/kendo.aspnetmvc.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo.modernizr.custom.js")"></script>
<script src="@Url.Content("~/Scripts/EditableGrids.js")"></script>
}
Here is the controller code:
public class LeasingController : System.Web.Mvc.Controller
{
private ILeasingRepository _repository;
public LeasingController() : this(new LeasingRepository())
{
}
public LeasingController(ILeasingRepository repository)
{
_repository = repository;
}
public ActionResult Index()
{
return View();
}
#region Lessors
public ActionResult GetLessors([DataSourceRequest]DataSourceRequest request)
{
LeasingModel model = new LeasingModel { Lessors = _repository.GetActiveLessors(SessionData.AccountNo) };
return Json(model.Lessors.ToDataSourceResult(request));
}
#endregion
#region leases
public ActionResult GetLeases([DataSourceRequest]DataSourceRequest request)
{
LeasingModel model = new LeasingModel { Leases = _repository.GetActiveLeases(SessionData.AccountNo) };
return Json(model.Leases.ToDataSourceResult(request));
}
public ActionResult SelectLease([DataSourceRequest]DataSourceRequest request, int lessorId)
{
SessionData.LessorId = lessorId;
return Json(new[] { FilteredLeases() }.ToDataSourceResult(request, ModelState),JsonRequestBehavior.AllowGet);
}
private List<Lease> FilteredLeases()
{
return _repository.GetActiveLeases(SessionData.AccountNo).Where(ls => ls.LessorId == SessionData.LessorId).ToList();
}
#endregion
Any help will be appreciated.
Thanks,
Dave Brown
P.S. I've also attached the through away project.