I don’t know what your Entity model looks like, but I suppose you have something like this model:
public partial class Project { public int Id { get; set; } public string Name { get; set; } public int Status { get; set; } }
and the Status property represents your enumeration value, then you have this enumeration:
public enum ProjectStatuses { Current = 1, Started = 2, Stopped = 3, Finished = 4, }
Then just create a new ViewModel like this:
public class ProjectDetails { public int Id { get; set; } public string Name { get; set; } public int Status { get; set; } public ProjectStatuses StatusValue { get { return (ProjectStatuses) Status; } }
And since I like extension methods, I will add the following:
public static class ModelListExtensions { public static IQueryable<ProjectDetails> ToViewModelDetails(this IQueryable<Project> modelList) { return modelList.Select(m => new ProjectDetails { Id = m.Id, Name = m.Name, Status = m.Status, }; } }
Update:
Here is the controller
public ActionResult Index() { int total; var viewModel = getGridList(out total); ViewBag.Total = total; return View(viewModel); } //this Action to get ajax pages [GridAction(EnableCustomBinding = true)] public ActionResult ReGetIndex(GridCommand command, int roleId) { int total; var list = getGridList(out total, roleId, command); return View(new GridModel {Data = list, Total = total}); } private IEnumerable<ProjectDetails> getGridList(out int total, GridCommand command = null) { command = command ?? new GridCommand {Page = 1}; foreach (var descriptor in command.SortDescriptors) { if (descriptor.Member == "StatusValue") descriptor.Member = "Status"; } foreach (FilterDescriptor descriptor in command.FilterDescriptors) { if (descriptor.Member == "StatusValue") descriptor.Member = "Status"; } var list = modelService.AllAsQuery() .ToViewModelDetails() // To convert it to our ViewModel if we have one .Where(command.FilterDescriptors); total = list.Count(); return (IEnumerable<ProjectDetails>) list.Sort(command.SortDescriptors) .Page(command.Page - 1, command.PageSize) .GroupBy(command.GroupDescriptors).ToIList(); }
And this is a view
@model IEnumerable<ProjectDetails> @{ Html.Telerik() .Grid(Model) .Name("ProjectsGrid") .Sortable() .Filterable() .EnableCustomBinding(true) .DataBinding(dataBinding => dataBinding .Ajax() .Select("ReGetIndex", "Projects")) .Pageable(page => page.Style(GridPagerStyles.PageSizeDropDown | GridPagerStyles.NextPreviousAndNumeric).Total(ViewBag.Total)) .Columns(column => { column.Bound(m => m.Id).Hidden(true); column.Bound(m => m.Name); column.Bound(m => m.StatusValue).ClientTemplate("<#= StatusName #>"); }) .Render(); }
Update:
If you want to apply at least one sort order, you can use something like this:
if (!command.SortDescriptors.Any()) { command.SortDescriptors.Add(new SortDescriptor {Member = "YourDefaultProperty"}); }