Child Grid data not filling when selecting Parent Grid row

4 posts, 0 answers
  1. Dave
    Dave avatar
    2 posts
    Member since:
    Aug 2013

    Posted 29 Aug 2013 Link to this post

    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.
  2. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2192 posts

    Posted 02 Sep 2013 Link to this post

    Hi Dave,

     
    I checked the provided project but it works as expected (screencast) - on each selection of master grid row, the child grid refreshes it;s data correctly. Could you please clarify what is the exact issue that you are experiencing? 

    Kind Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Dave
    Dave avatar
    2 posts
    Member since:
    Aug 2013

    Posted 02 Sep 2013 Link to this post

    Hello Vladimir,

    I must have not been clear. The attached project that is working the way I would like the grids to work. The pasted in code is the code that is not working.

    I did find the problem and fix but I'm not sure why it was not working.

    I had the code like this:

    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();
    }

    But changed it to this and it is working now:

    public ActionResult SelectLease([DataSourceRequest]DataSourceRequest request, int lessorId)
    {
    LeasingModel model = new LeasingModel { Leases = _repository.GetActiveLeases(SessionData.AccountNo) };
     
    SessionData.LessorId = lessorId;
    return Json(model.Leases.Where(ls => ls.LessorId == lessorId).ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
     }

    I am curious why it was not working.

    Thanks,
    Dave Brown


  4. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2192 posts

    Posted 04 Sep 2013 Link to this post

    Hi Dave,

     
    From the provided information it's not clear for us what is the exact reason for current behavior - could you please update the previously provided project to replicate the issue and send it back to us? This would help us pinpoint the exact reason for this behavior.

    Kind Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top