Filtering

It is your responsibility to add whatever inputs to your page that you want for filtering. Then call the MVCGrid.setFilters javascript function to perform the filtering.

Or use client-side bindings, as is done below:

Id Last Name First Name Status
370AdamsPatrickInactive
493AdamsKarenActive
570AdamsNancyInactive
599AdamsEricInactive
635AdamsChristineInactive
663AdamsBonnieInactive
778AdamsRonaldInactive
820AdamsSeanInactive
897AdamsJudithActive
857AlexanderMichaelActive
Showing 1 to 10 of 1000 entries

Code

View
<div class="well">
    <div class="form-inline">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Last Name"
                   data-mvcgrid-type="filter" data-mvcgrid-option="LastName" />
        </div>
        <div class="form-group">
                
                
            <input type="email" class="form-control" placeholder="First name"
                   data-mvcgrid-type="filter" data-mvcgrid-option="FirstName" />
        </div>
        <div class="form-group">
            <select class="form-control" data-mvcgrid-type="filter" data-mvcgrid-option="Status">
                <option value=""></option>
                <option value="Active">Active</option>
                <option value="Inactive">Inactive</option>
            </select>
        </div>
        <button type="button" class="btn btn-default" data-mvcgrid-apply-filter="click">Apply</button>
    </div>
</div>

@Html.MVCGrid("Filtering")
        
Inside MVCGridConfig.cs
MVCGridDefinitionTable.Add("Filtering", new MVCGridBuilder<Person>(colDefauls)
    .WithAuthorizationType(AuthorizationType.AllowAnonymous)
    .AddColumns(cols =>
    {
        cols.Add("Id").WithSorting(false)
            .WithValueExpression(p => p.Id.ToString());
        cols.Add("LastName").WithHeaderText("Last Name")
            .WithValueExpression(p => p.LastName)
            .WithFiltering(true);
        cols.Add("FirstName").WithHeaderText("First Name")
            .WithValueExpression(p => p.FirstName)
            .WithFiltering(true);
        cols.Add("Status").WithSortColumnData("Active")
            .WithHeaderText("Status")
            .WithValueExpression(p => p.Active ? "Active" : "Inactive")
            .WithFiltering(true);
    })
    .WithSorting(true, "LastName")
    .WithPaging(true, 10, true, 100)
    .WithFiltering(true)
    .WithRetrieveDataMethod((context) =>
    {
        var options = context.QueryOptions;
        int totalRecords;
        var repo = DependencyResolver.Current.GetService<IPersonRepository>();
        bool? active = null;
        string fa = options.GetFilterString("Status");
        if (!String.IsNullOrWhiteSpace(fa))
        {
            active = (String.Compare(fa, "active", true) == 0);
        }
        string sortColumn = options.GetSortColumnData<string>();
        var items = repo.GetData(out totalRecords,
            options.GetFilterString("FirstName"),
            options.GetFilterString("LastName"),
            active,
            options.GetLimitOffset(), options.GetLimitRowcount(),
            sortColumn, options.SortDirection == SortDirection.Dsc);
        return new QueryResult<Person>()
        {
            Items = items,
            TotalRecords = totalRecords
        };
    })
);