1

I am trying to implement a grid using MVCGrid.net

In the MVCGridConfig.cs they have something like this

 .WithRetrieveDataMethod((context) =>
         {
             var options = context.QueryOptions;
             int totalRecords;
             var repo = DependencyResolver.Current.GetService<IPersonRepository>();
             ...
          }

I do not quite understand this IPersonRepository. Please let me know what this interface is supposed to do?

Initially I was using WebGrid, where I was passing List of customer to the view and in the view had IEnumerable to view the grid. If the user had a search term, I would pass that search term to the controller which would accordingly return list of customers matching the search term.

I am not able to relate how to implement the WebGrid logic into this WithRetrieveDataMethod. Specifically, what I should do about that interface.

Also, if there is a search term being passed from the view, how can I change the WithRetrieveDataMethod.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
blue piranha
  • 3,706
  • 13
  • 57
  • 98

1 Answers1

0

This IPersonRepository is used for global search, sorting and filtering of columns in grid. You need to change 'filterFirstName', 'filterLastName' etc to corresponding elements that you use in your project and you can also make other changes. In Case section also you need only properties that you are using in your project.

public interface IPersonRepository
{
    IEnumerable<Person> GetData(out int totalRecords, string globalSearch, int? limitOffset, int? limitRowCount, string orderBy, bool desc);
    IEnumerable<Person> GetData(out int totalRecords, int? limitOffset, int? limitRowCount, string orderBy, bool desc);
    IEnumerable<Person> GetData(out int totalRecords, string filterFirstName, string filterLastName, bool? filterActive, int? limitOffset, int? limitRowCount, string orderBy, bool desc);
}
public class PersonRepository : IPersonRepository
{
    public IEnumerable<Person> GetData(out int totalRecords, string filterFirstName, string filterLastName, bool? filterActive, int? limitOffset, int? limitRowCount, string orderBy, bool desc)
    {
        return GetData(out totalRecords, null, filterFirstName, filterLastName, filterActive, limitOffset, limitRowCount, orderBy, desc);
    }

    public IEnumerable<Person> GetData(out int totalRecords, string globalSearch, int? limitOffset, int? limitRowCount, string orderBy, bool desc)
    {
        return GetData(out totalRecords, globalSearch, null, null, null, limitOffset, limitRowCount, orderBy, desc);
    }

    public IEnumerable<Person> GetData(out int totalRecords, string globalSearch, string filterFirstName, string filterLastName, bool? filterActive, int? limitOffset, int? limitRowCount, string orderBy, bool desc)
    {
        using (var db = new SampleDatabaseEntities())
        {
            var query = db.People.AsQueryable();

            if (!String.IsNullOrWhiteSpace(filterFirstName))
            {
                query = query.Where(p => p.FirstName.Contains(filterFirstName));
            }
            if (!String.IsNullOrWhiteSpace(filterLastName))
            {
                query = query.Where(p => p.LastName.Contains(filterLastName));
            }
            if (filterActive.HasValue)
            {
                query = query.Where(p => p.Active == filterActive.Value);
            }

            if (!String.IsNullOrWhiteSpace(globalSearch))
            {
                query = query.Where(p => (p.FirstName + " " + p.LastName).Contains(globalSearch));
            }

            totalRecords = query.Count();

            if (!String.IsNullOrWhiteSpace(orderBy))
            {
                switch (orderBy.ToLower())
                {
                    case "firstname":
                        if (!desc)
                            query = query.OrderBy(p => p.FirstName);
                        else
                            query = query.OrderByDescending(p => p.FirstName);
                        break;
                    case "lastname":
                        if (!desc)
                            query = query.OrderBy(p => p.LastName);
                        else
                            query = query.OrderByDescending(p => p.LastName);
                        break;
                    case "active":
                        if (!desc)
                            query = query.OrderBy(p => p.Active);
                        else
                            query = query.OrderByDescending(p => p.Active);
                        break;
                    case "email":
                        if (!desc)
                            query = query.OrderBy(p => p.Email);
                        else
                            query = query.OrderByDescending(p => p.Email);
                        break;
                    case "gender":
                        if (!desc)
                            query = query.OrderBy(p => p.Gender);
                        else
                            query = query.OrderByDescending(p => p.Gender);
                        break;
                    case "id":
                        if (!desc)
                            query = query.OrderBy(p => p.Id);
                        else
                            query = query.OrderByDescending(p => p.Id);
                        break;
                    case "startdate":
                        if (!desc)
                            query = query.OrderBy(p => p.StartDate);
                        else
                            query = query.OrderByDescending(p => p.StartDate);
                        break;
                }
            }


            if (limitOffset.HasValue)
            {
                query = query.Skip(limitOffset.Value).Take(limitRowCount.Value);
            }

            return query.ToList();
        }
    }

    public IEnumerable<Person> GetData(out int totalRecords, int? limitOffset, int? limitRowCount, string orderBy, bool desc)
    {
        return GetData(out totalRecords, null, null, null, limitOffset, limitRowCount, orderBy, desc);
    }
}
Anoopkumar
  • 96
  • 1
  • 12
  • Ok for the i mplementation but is not possible to filter "server side"? And in client side case, how can i avoid to get parameter on url? – Emanuele Jan 25 '17 at 14:43