gpt4 book ai didi

asp.net-mvc - 在 Asp MVC Telerik Grid 和 Entity Framework 中处理枚举

转载 作者:行者123 更新时间:2023-12-04 16:26:03 25 4
gpt4 key购买 nike

对于开发中的 Web 应用程序(ASP.Net MVC),我使用的是 Telerik 网格。网格绑定(bind)到我的列表的 IQueryable,因为它是一个大表,我希望 Telerik 在列表上应用它的过滤器,然后执行这个结果,而不是下载 10'000 行(带有连接的表),然后使用过滤器,仅使用行。

我正在使用(我真的需要它用于此页面,它是关键功能之一)网格的过滤器/顺序。

主要列之一(确定数据的种类)是枚举。

问题是我得到了 "Specified type member is not supported in linq to entities"一旦我试图过滤/排序它。

我必须将它绑定(bind)到枚举(而不是映射的 int)上,因为如果我使用 id,filters/order by 将在一个 int 上,我不能指望用户知道外部表的 id。

我只是无法再次实现自己的所有网格参数(位于 url 中)(我假设,要么我什么都做,要么什么都不做)并正确过滤,正确排序)。

您有解决方法的想法吗?

最佳答案

我不知道你的实体模型是什么样子,但我假设你有这样的模型:

public partial class Project
{
public int Id { get; set; }
public string Name { get; set; }
public int Status { get; set; }
}

并且 Status 属性代表你的枚举值,那么你有这个枚举:
public enum ProjectStatuses
{
Current = 1,
Started = 2,
Stopped = 3,
Finished = 4,
}

然后像这样创建新的 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 ); } }
}

因为我喜欢扩展方法,所以我会添加这个:
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,
};
}
}

更新:

这是 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();
}

这是 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();
}

更新:

如果您想强制执行至少一种排序顺序,您可以使用如下内容:
 if (!command.SortDescriptors.Any())
{
command.SortDescriptors.Add(new SortDescriptor {Member = "YourDefaultProperty"});
}

关于asp.net-mvc - 在 Asp MVC Telerik Grid 和 Entity Framework 中处理枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com