Skip to content

Instantly share code, notes, and snippets.

@theorigin
Created September 5, 2012 17:31
Show Gist options
  • Save theorigin/3640686 to your computer and use it in GitHub Desktop.
Save theorigin/3640686 to your computer and use it in GitHub Desktop.
public class SomeViewModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
int currentPage = 1;
int itemsPerPage = 8;
var page = bindingContext.ValueProvider.GetValue("page");
if (page != null)
{
int.TryParse(page.AttemptedValue, out currentPage);
}
var recordsPerPage = bindingContext.ValueProvider.GetValue("rp");
if (recordsPerPage != null)
{
int.TryParse(recordsPerPage.AttemptedValue, out itemsPerPage);
}
var query = bindingContext.ValueProvider.GetValue("query");
var orderBy = bindingContext.ValueProvider.GetValue("sortname");
var sortOrder = bindingContext.ValueProvider.GetValue("sortorder");
var model = base.BindModel(controllerContext, bindingContext) as SomeViewModel;
if (model != null)
{
model.Query = query == null ? string.Empty : query.AttemptedValue;
model.CurrentPage = currentPage;
model.ItemsPerPage = itemsPerPage;
model.OrderBy = orderBy == null ? string.Empty : orderBy.AttemptedValue;
model.OrderDirection = sortOrder == null ? string.Empty : sortOrder.AttemptedValue;
return model;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment