We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Pass through search parameter from ej-grid to controller method

Hi,

I am trying to pass a search parameter from my grid to the contoller method. I have seen some search solution using the DataManager, however, they all
appear to be client side searching. Is there a way to pass the parameter to the controller method? Also is it possible to pass more than one parameter?

Below is my code, I want to perform a search for a person by firstname or lastname.


Thanks


// CSHTML

 <ej-grid id="personsGrid" allow-resize-to-fit="true" allow-paging="true" allow-selection="true">
     <e-datamanager url="PersonSearch" adaptor="WebApiAdaptor"></e-datamanager>
     <e-edit-settings allow-searching="true"></e-edit-settings>
     <e-page-settings enable-query-string="true"> </e-page-settings>
     <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> {"search"}' />
     <e-columns>
         <e-column field="Id" visible="false" is-primary-key="true" header-text="" text-align="Left"></e-column>
         <e-column field="FirstName" allow-editing="false" header-text="First Name" text-align="Left"></e-column>
        <e-column field="LastName" header-text="Last Name" text-align="Left"></e-column>
      </e-columns>
 </ej-grid> 

// CONTROLLER


[HttpGet]
        public JsonResult PersonSearch(string searchValue)
        {
            var persons = _personService.SearchPerson(searchValue);
            var result = new { result = persons, count = persons.Count() };
            return Json(result);
        }

1 Reply

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team July 3, 2017 05:29 PM UTC

Hi Zelda, 

Thanks for contacting syncfusion support. 

We have checked your query and according to your requirement we have prepared sample on WebAPI Adaptor and  perform searching on serverside. Please refer to the code example. 


@(Html.EJ().Grid<object>("Grid") 
              .Datasource(ds => ds.URL("/api/Orders").Adaptor(AdaptorType.WebApiAdaptor)) 
              .AllowPaging() //Paging 
              .PageSettings(page => { page.PageSize(8); }) 
              .EnableAltRow() 
               .ToolbarSettings(toolbar => { toolbar.ShowToolbar().ToolbarItems(items => { items.AddTool(ToolBarItems.Search); }); }) 
              .Columns(col => 
              { 
                  col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(90).Add(); 
                  col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add(); 
                   
              }) 
         ) 
Serverside:- 
public object Get() 
        { 
            List<OrdersView> ord = db.OrdersViews.ToList(); 
            var queryString = HttpContext.Current.Request.QueryString; 
            int skip = Convert.ToInt32(queryString["$skip"]); 
            int take = Convert.ToInt32(queryString["$top"]); 
            string sort =queryString["$orderby"];   
            var filter = queryString["$filter"]; 
            if (filter != null) 
            { 
                string key; 
                if (filter.Contains("substring")) //searching 
                { 
                    key = filter.Split(new string[] { "'" }, StringSplitOptions.None)[1]; 
                    ord = ord.Where(fil => fil.CustomerID.Contains(key.ToUpper()) || fil.EmployeeID.ToString().Contains(key) || fil.Freight.ToString().Contains(key) || fil.OrderID.ToString().Contains(key) || fil.ShipCountry.Contains(key)).Distinct().ToList(); 
                } 
            } 
            else 
            { 
 
                ord = ord.Skip(skip).Take(take).ToList(); 
            } 
            var count = ord.Count; 
            return new {Items = ord, Count = count }; 
        }         
        

 If you want to pass additional parameters using the DataManager. We can achieve it using the addParams method of the ej.Queryin “Query” Grid property. In the controller we have processed the data based on the query string value and rendered the data on the grid.  



Refer to the code example. 
@(Html.EJ().Grid<object>("Grid") 
              .Datasource(ds => ds.URL("/api/Orders").Adaptor(AdaptorType.WebApiAdaptor)) 
              .AllowPaging() //Paging 
              .Query("new ej.Query().addParams('data', 'Bern')")   
               .ToolbarSettings(toolbar => { toolbar.ShowToolbar().ToolbarItems(items => { items.AddTool(ToolBarItems.Search); }); }) 
              .Columns(col => 
              { 
                  col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(90).Add(); 
                  col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add(); 
                   
  
             }) 
Serverside:- 
                 public ActionResult GetData(DataManager dm, string data)  
        {  
             
        } 


Please get back to us if you need any further assistance. 

Regards, 

Farveen sulthana T 


Loader.
Live Chat Icon For mobile
Up arrow icon