Skip to content
Leonid Gordo edited this page May 9, 2012 · 2 revisions

Custom view models

MvcBlanketLib simplifies the creation of the pages with sorting and paging support. This is achieved with using of PagedViewModel<> class and PagedViewModelFactory class factory.

PagedViewModel class is the basis of the concept. This class encapsulates the sorting and paging features extending those provided by MvcContrib library.

This class provides the following properties and methods:

Methods:

  • AddFilter (deprecated, use the filters model instead)
  • Apply
  • Setup
  • SetupByExpressions
  • SetupByNames
  • Skip

All methods return the instance of the PagedViewModel<> class, so the expressions can be joined together using the fluent syntax.

Properties:

  • ControllerContext
  • Query
  • GridSortOptions
  • DefaultSortColumn
  • DefaultSortDirection
  • PagedList
  • Page
  • PageSize
  • ViewData

AddFilter method

This method has the following overloads:

  • PagedViewModel<T> AddFilter(Expression<Func<T, bool>> predicate)

Appends the condition given in the predicate parameter to the query.

  • PagedViewModel<T> AddFilter<TValue>(string key, TValue value, Expression<Func<T, bool>> predicate)

Appends the condition in the predicate parameter to the query if value is not null or (if value is the string) the value is not empty string . Appends key and value parameters to the view data dictionary.

  • PagedViewModel<T> AddFilter<TValue>(string keyField, object value, Expression<Func<T, bool>> predicate, IQueryable<TValue> query, string textField)

Appends the condition in the predicate parameter to the query if value is not null or (if value is the string) the value is not empty string . Converts the given query to the select list (SelectList class instance) and puts the select list into the view data dictionary.

Apply method

This method has the following overloads:

  • PagedViewModel<T> Apply(IQueryable<T> data)

Saves the given query into the instance private field.

  • PagedViewModel<T> Apply(Func<Dictionary<string, string>, IQueryable<T>> selector)

Invokes the selector given passing to the callee the filters as Dictionary<string, string> and saves the result into the instance private field.

  • PagedViewModel<T> Apply(Func<IPageFiltersModel, IQueryable<T>> selector)

Invokes the selector given passing to the callee the strongly typed filters model as interface and saves the result into the instance private field.

  • PagedViewModel<T> Apply<TS>(Func<TS, IQueryable<T>> selector)

Invokes the selector given passing to the callee the strongly typed filters model and saves the result into the instance private field.

Setup and its derivatives method

This method has the following overloads:

  • PagedViewModel<T> Setup()

Executes the query using sort specified in the GridSortOptions property or using default sorting options given in the DefaultSortColumn and DefaultSortDirection properties. Applies the pagination using page number in the Page (if not null) or DefaultPageNumber properties and PageSize (if not null) and DefaultPageSize properties. Puts the result into the PagedList property of IPagination<> type.

  • PagedViewModel<T> Setup<TK>(Func<T, TK> order)

Executes the query using sort direction specified in the GridSortOptions property and order function given. Applies the pagination using page number in the Page (if not null) or DefaultPageNumber properties and PageSize (if not null) and DefaultPageSize properties. Puts the result into the PagedList property of IPagination<> type.

  • PagedViewModel<T> SetupByExpressions(params Expression<Func<T, object>>[] orderLambdas)

Executes the query using sort direction specified in the GridSortOptions property and orderLambdas functions given in the order they passed to the method. Applies the pagination using page number in the Page (if not null) or DefaultPageNumber properties and PageSize (if not null) and DefaultPageSize properties. Puts the result into the PagedList property of IPagination<> type.

  • PagedViewModel<T> SetupByNames(params string[] columnNames)

Executes the query using sort direction specified in the GridSortOptions property and columnNames given or default using default sorting options given in the DefaultSortColumn and DefaultSortDirection properties. Applies the pagination using page number in the Page (if not null) or DefaultPageNumber properties and PageSize (if not null) and DefaultPageSize properties. Puts the result into the PagedList property of IPagination<> type. Support the mapping feature and multiple columns sorting for each given column name using SortMapping action filter attribute.

Skip method

This method provides the detour around the standard pipeline using Apply and Setup methods in the order. This method simply stores given IPagination<> data into the PagedList property.

ControllerContext property

Stores controller context of ControllerContext type used to access ViewData dictionary and HttpContext entries (used to store sort mappings).

Query property

Stores the query. Usually manipulated via Apply and Setup methods calls.

GridSortOptions property

Stores setting for sorting options. Contains Columns and Direction properties that determine sort field and direction.

DefaultSortColumn property

Stores the sort column name if user does not select any explicit column for sorting. Usually occurs at the page being rendered for the first time.

DefaultSortDirection property

Stores the sort direction if user does not select any explicit direction for sorting. Usually occurs at the page being rendered for the first time.

PagedList property

Stores the the query with sorting and paging optionally applied. Usually accessed for reading after setting full processing pipeline using Apply and Setup methods calls.

Page property

Stores the current page number for paging.

PageSize property

Stores the page size for paging

ViewData property

Stores the reference to view data dictionary. If not explicitly set then the property returns the reference using current ControllerContext.