Grid Dropdown Filter: Populate dropdown from the model object

2 posts, 0 answers
  1. Rohit
    Rohit avatar
    14 posts
    Member since:
    Jan 2015

    Posted 22 Jan 2015 Link to this post

    Hi All,

    Thanks Telerik for addressing my issues. I am stuck with another problem.

    I want to populate the grid with dropdown filters from the model object. 
    Currently, my Controller does the Database call and return data is binded to the kendo grid. 
    Another call to DB is done as a action from the view page of the grid to populate the dropdown. 
    I want to avoid another DB call to populate the drowndown in the grid. It would be ideal to retrieve the data from the model object and populate the dropdown in grid.

    Following is my approach.

    Controller: 

      public ActionResult AllMessages()
            {
                List<LogModel> logs = this.GetAllMessages();
               
                return View(logs);
            
            }

            public ActionResult FilterMenuCustomization_ReceivePorts(LogModel logsinput)
            {

                List<LogModel> logs = this.GetAllMessages();
                return Json(logs.Select(e => e.ReceivePortName).Distinct(), JsonRequestBehavior.AllowGet);
               
            }

      private List<LogModel> GetAllMessages()
            {
                DataAccess dataAccess = new DataAccess(_LogDBConnectionString);
                DataTable dt = dataAccess.GetAllMessages();
                List<LogModel> outboundMessages = dt.AsEnumerable().ToList().ConvertAll(x => new LogModel
                {
                    ReceivePortName = (string)x.ItemArray[0],
                    SendPortName = (string)x.ItemArray[1],
                    ControlID = (string)x.ItemArray[2],
                    SenderID = (string)x.ItemArray[3],
                    ReceiverID = (string)x.ItemArray[4],
                    loggedDate = (DateTime)x.ItemArray[5],
                    LogID = x.ItemArray[7].ToString(),
                    ReplayedCount = (int)x.ItemArray[8],
                    InterchangeID = x.ItemArray[9].ToString(),
                    AckCode = (string)x.ItemArray[10],
                    RetryCount = (int)x.ItemArray[11]
                });

                return outboundMessages;
            }

    View:

     @using (Html.BeginForm("ReplaySelectedInboundMessages", "Home"))
       {
        <div id="gridContent">
            <h1>All Messages</h1>  
        @(Html.Kendo().Grid<ViaPath.MessageReplay.MvcApp.Models.LogModel>(Model)
        .Name("gridTable")   
        .HtmlAttributes(new {style = "font-family: verdana,arial,sans-serif; font-size: 11px;color: #333333;border-color: #999999;"})   
        
        .Columns(columns => 
            {
              columns.Template(@<text><input class="box"  id="assignChkBx" name="assignChkBx" type="checkbox" value="@item.LogID"/></text>).HeaderTemplate(@<text><input class="selectAll" type="checkbox" id="allBox" onclick="toggleSelection()"/></text>).Width(20);
                //columns.Template(p => { }).ClientTemplate("<input type='checkbox' #= CheckSelect ? checked='checked':'' # class='chkbx' />");
                        
                columns.Bound(p => p.LogID).Template(p => Html.ActionLink(((string)p.LogID), "MessageDetails", new { logid = p.LogID })).Width(200);
                columns.Bound(p => p.ReceivePortName).Width(100).Filterable(ft => ft.UI("ReceivePortsFilter")); 
                columns.Bound(p => p.SendPortName).Width(100);
                columns.Bound(p => p.loggedDate).Format("{0:MM/dd/yyyy hh:mm tt}").Filterable(f => f.UI("DateTimeFilter").Extra(true)).Width(100);
                columns.Bound(p => p.ControlID).Width(100);
                columns.Bound(p => p.SenderID).Width(100);
                columns.Bound(p => p.ReceiverID).Width(100);
                columns.Bound(p => p.InterchangeID).Width(100);
                columns.Bound(p => p.ReplayedCount).Width(100);
                columns.Bound(p => p.RetryCount).Width(100);
                columns.Bound(p => p.AckCode).Width(20);
            })
               // .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
                .Filterable()
                .Pageable(page => page.PageSizes(new int[]{10,25,50,100}).Enabled(true))
                .Sortable()
                .Scrollable(src => src.Height("auto"))
                .Resizable(resize => resize.Columns(true))
               

                )

             <br />
            <br />
            <input type="Submit" name="btnReplayMessage" value="Replay" title="Replay Message" \>
    </div>  
          <script type="text/javascript">
              function ReceivePortsFilter(element) {
                  element.kendoDropDownList({
                      dataSource: {
                          transport: {
                              read: "@Url.Action("FilterMenuCustomization_ReceivePorts")"
                }
            },
            optionLabel: "--Select Value--"
        });
    }
          </script>
           
       }



  2. Rohit
    Rohit avatar
    14 posts
    Member since:
    Jan 2015

    Posted 22 Jan 2015 Link to this post

    Please ignore the thread.
    I got it working by modifying the controller under the following way. I added a session object and accessing the session object in the action function of the dropdown.

     public ActionResult AllMessages()
            {
                List<LogModel> logs = this.GetAllMessages();
                Session["ReceivePortsList"] = logs.Select(e => e.ReceivePortName).Distinct();
                return View(logs);
            
            }

            public ActionResult FilterMenuCustomization_ReceivePorts(LogModel logsinput)
            {
                
               // List<LogModel> logs = this.GetAllMessages();
                return Json(Session["ReceivePortsList"], JsonRequestBehavior.AllowGet);
               
            }
Back to Top