This is a migrated thread and some comments may be shown as answers.

Grid Dropdown Filter: Populate dropdown from the model object

1 Answer 110 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rohit
Top achievements
Rank 1
Rohit asked on 22 Jan 2015, 03:07 PM
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>
       
   }



1 Answer, 1 is accepted

Sort by
0
Rohit
Top achievements
Rank 1
answered on 22 Jan 2015, 03:48 PM
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);
           
        }
Tags
Grid
Asked by
Rohit
Top achievements
Rank 1
Answers by
Rohit
Top achievements
Rank 1
Share this question
or