0
I have grid in index page and i have another grid in Data page. 

On grid on Index page i clicked click view record and then redirect to Data page which also contain the grid. My question is how i can filter grid in Data page based on selected record in grid in Index page.

As you can see, in method GetAllList i tried to filter the grid using rListID which come from the grid on Index page.

Please advise how i can achieve this. Thank you

Index page (View)

    @(Html.Kendo().Grid<HApp.Models.SModel>()

        .Name("Grid")
        .HtmlAttributes(new { @Style = "align:center; font-size:10.5px; length:100%" })
        .Columns(columns =>
        {

            columns.Bound(p => p.RListID).Visible(false);
            columns.Bound(p => p.TListID).Visible(false);
            columns.Command(commands => commands.Edit()).Width(175);
            columns.Command(command => command.Custom("View").Click("OnshowDetails")).Width(150);


        })


        .Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
        .Pageable()
        .Scrollable()
        .DataSource(dataSource => dataSource
            .Ajax()//bind with Ajax instead server bind
            .PageSize(10)
            .ServerOperation(true)
                .Model(model => model.Id(p => p.RListID))


            .Read(read => read.Action("GetCData", "CDetails").Type(HttpVerbs.Get))

        )
        //.Events(events => events
        //                        .Change("change"))



    )


    <script type="text/javascript">


        function OnshowDetails(e) {
           var grid = $('#Grid').data('kendoGrid');   //get a reference to the grid data 
           var record = grid.dataItem(grid.select()); //get a reference to the currently selected row
           var rListID = record.RListID;

           window.location.href = "@Url.Action("Data ", "CDetails")" + "/?rListID =" + rListID ; 




        }
    </script>


Data Page View

@(Html.Kendo().Grid<HApp.Models.SListsModel>()

    .Name("SList")
    .HtmlAttributes(new { @Style = "align:center; font-size:10.5px; length:100%" })
    .Columns(columns =>
    {
        columns.Bound(p => p.RListID).Visible(false);
        columns.Bound(p => p.CCID);



        columns.Command(commands => commands.Edit()).Width(175);      
    })


    .Pageable()
    .Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()//bind with Ajax instead server bind
        .PageSize(5)
        .ServerOperation(true)
        .Model(model => model.Id(p => p.CID))

        .Read(read => read.Action("GetListData", "CDetails").Type(HttpVerbs.Get))

    )
    .Events(events => events
                            .Change("change"))



)


Controller:

    public ActionResult Index()
    {

                return View();

    }

    public ActionResult Detail()
    {

            return View();
    }


/// <summary>
        /// Bind to GetListData
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ActionResult GetListData([DataSourceRequest] DataSourceRequest request)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            return Json(GetAllList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

         private static IEnumerable<SListsModel> GetAllList(Guid rListID)
        {


            var context = new HEntities();

            return context.SLists
                .Where(filter => filter.RListID== rListID)
                .Select(s_list => new SessionListsModel
                {

                    RListID = s_list.RListID,
                    CCID = s_list.CCID,



                });

        }



 public ActionResult GetCData([DataSourceRequest] DataSourceRequest request)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            return Json(GetAllComList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Get all available session from Session table
        /// </summary>
        /// <returns></returns>
        private static IEnumerable<SModel> GetAllComList()
        {
            var context = new HEntities();

            return context.SM
                .Select(com_list => new SModel
                {
                    RListID = com_list.RListID,
                    PortID = com_list.PortID ,



                });

        }
Supermode
  • 924
  • 3
  • 32
  • 53

1 Answers1

1

To set initial filtering of the Grid I suggest you to use the Filter method of the DataSource configurator object.

.DataSource(dataSource => dataSource
            .Ajax()                
            .Filter(flt=>flt.Add(c=>c.RListID).EndsWith(rListIDValuePassedFromController))
Petur Subev
  • 19,983
  • 3
  • 52
  • 68
  • Hi XMR, Thank you for your quick response.Currently i am not sure how do i got value from control (rListIDValuePassedFromController). Please note: As you can see, in method GetAllList i tried to filter the grid using rListID which come from the grid on Index page. That where i would like to pass rListID value but currently return no value. Therefore, i am still confuse how i can get rListIDValuPassedFromController. Unless if there is some way i can access value from : public ActionResult Detail() { return View(); } – Supermode Nov 14 '12 at 20:36
  • Basically to transfer objects from Controller to the View you should use the ViewData or the ViewBag. I am still not sure do you have that Guid rListID value in the controller at all or it is null? – Petur Subev Nov 14 '12 at 20:50
  • I have tried the following but still not working .Filter(filter => filter.Add(p => p.RListID).Equals(ViewBag.RListID)) and in the controller: public ActionResult Detail(Guid rListID) { ViewBag.RListID = rListID; return View(); } – Supermode Nov 14 '12 at 23:58
  • This article help me solve the problem: http://stackoverflow.com/questions/12354821/mvc-handling-a-corpid-for-the-site – Supermode Nov 16 '12 at 02:12