Created
September 5, 2012 17:31
-
-
Save theorigin/3640686 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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