J4N*_*J4N 4 asp.net-mvc enums entity-framework telerik telerik-grid
对于开发中的Web应用程序(ASP.Net MVC),我正在使用telerik网格.网格绑定到我的列表的IQueryable,因为它是一个大表,我希望telerik在列表中应用它的过滤器,然后执行此结果,而不是下载10'000行(使用连接的表),然后使用过滤器,仅使用行.
我正在使用(我真的需要它用于此页面,这是关键功能之一)网格的过滤器/顺序.
主列之一(确定数据类型)是枚举.
问题是,"Specified type member is not supported in linq to entities"一旦我尝试过滤/排序,我就会得到一个.
我要将它绑定在枚举(而不是映射的int)上,因为如果我使用id,filter/order by将在int上,我不能指望用户知道外表的id.
我只是无法再次实现自己所有网格参数(位于网址)(我假设,它要么我做所有事情,要么什么都没有)并正确过滤它,正确地订购它).
你有解决方法吗?
我不知道你的实体模型是怎样的,但我想你会有类似这样的模型:
public partial class Project
{
public int Id { get; set; }
public string Name { get; set; }
public int Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并且Status属性表示你的枚举值,那么你就是这个枚举:
public enum ProjectStatuses
{
Current = 1,
Started = 2,
Stopped = 3,
Finished = 4,
}
Run Code Online (Sandbox Code Playgroud)
然后就像这样创建新的ViewModel:
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; } }
// This property to display in telerik ClientTemplate
public string StatusName { get { return Enum.GetName(typeof (ProjectStatuses), Status ); } }
}
Run Code Online (Sandbox Code Playgroud)
因为我喜欢扩展方法,我会添加这个:
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,
};
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
这是控制器
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();
}
Run Code Online (Sandbox Code Playgroud)
这就是视图
@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();
}
Run Code Online (Sandbox Code Playgroud)
更新:
如果要强制执行至少一个排序顺序,可以使用以下内容:
if (!command.SortDescriptors.Any())
{
command.SortDescriptors.Add(new SortDescriptor {Member = "YourDefaultProperty"});
}
Run Code Online (Sandbox Code Playgroud)