如何构建控制器多个条件进行排序asp.net的MVC [英] How to structure controller to sort multiple criteria asp.net mvc

查看:163
本文介绍了如何构建控制器多个条件进行排序asp.net的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是建立一个控制器通过很多(可能为null)标准进行排序的最好方法?比方说,我正在构建一个卖汽车的网站。我CarController有一个功能指数()返回车的一个IList的观点,并在每节车厢的细节与局部视图渲染。

What's the best way to set up a controller to sort by many (possibly null) criteria? Say for example, I was building a site that sold cars. My CarController has a function Index() which returns an IList of cars to the view, and details on each car are rendered with a partial view.

什么是结构最好的方法?尤其是如果有很多的条件:车型(又名SUV),汽车品牌,汽车模型,汽车年,汽车价格,汽车颜色,布尔是否新款,或者如果我想通过最近的排序我等......我中号使用NHibernate作为我的ORM。我应该只是一吨的可能NHibernate的查询,并找出该选的基础上,如果/然后是在控制​​?还是有更简单的方法。

What's the best way to structure this? Especially if there are a lot of criteria: Car Type (aka SUV), Car Brand, Car Model, Car Year, Car Price, Car Color, bool IsNew, or if I want to sort by closest to me etc... I'm using NHibernate as my ORM. Should I have just a ton of possible NHibernate queries and figure out which one to choose based on if/then's in the controller? Or is there a simpler way.

感谢这么多的帮助。

推荐答案

我在我的系统任务和创建的特殊类搜索:

I have tasks in my system and created special class for searching:

public class TaskSearchCriteria
{
    public List<int> Statuses { get; set; }
    public List<int> Severities { get; set; }
    public List<int> CreatedBy { get; set; }
    public List<int> ClosedBy { get; set; }
    public List<int> LastModificationBy { get; set; }

    public DateTime? CreationDateFrom { get; set; }
    public DateTime? CreationDateTo { get; set; }

    public bool SearchInComments { get; set; }
    public bool SearchInContent { get; set; }
    public bool SearchInTitle { get; set; }
}

在这个类的一个方法适用于过滤器:

One method in that class applies filters:

public IQueryable<Task> Filter(IQueryable<Task> tasks)
{
    return
        FilterDates(
        FilterAssignedTo(
        FilterGroups(
        FilterOperationSystems(
        FilterPlatforms(
        FilterPriorities(
        FilterSeverities(
        FilterVersionsResolved(
        FilterVersionsReported(
        FilterTaskContent(
        FilterStatuses(tasks)))))))))));
}

这是用于过滤方法之一:

This is one of methods used for filtering:

private IQueryable<Task> FilterSeverities(IQueryable<Task> tasks)
{
    if (Severities.Contains(TaskSearchConsts.All) || (!Severities.Any()))
        return tasks;

    var expressions = new List<Expression>();

    if (Severities.Contains(TaskSearchConsts.Active))
        expressions.Add(_taskParameter.EqualExpression("Severity.IsActive", 1));

    return tasks.WhereIn(_taskParameter, "Severity.ID", Severities, expressions);
}

这是实体框架,但它可以在NHibernate中也加入where子句来IQUERY容易。

This is Entity Framework, but it can be easily done in nHibernate too by adding where clauses to IQuery.

要搜索我在控制器的方法:

To search I have a method in controller:

[HttpPost]
public ActionResult TaskSearch([Bind(Prefix = "Search")]TaskSearchCriteria criteria)

有过滤一级是好的,因为它可以被序列化并保存到数据库,以备将来使用。这样,用户可以重新使用滤波器参数。

Having one class for filtering is nice, because it can be serialized and save to database for future use. This way user can reuse filter parameters.

您将有:

public class CarSearchCriteria
{
    List<int> ListOfCarTypesIds;
    List<int> ListOfCarBrandIds;
    bool IsNew;
    //and more
}

如果列表是空的,不应用过滤器。

If list is empty, you don't apply filter.

这篇关于如何构建控制器多个条件进行排序asp.net的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆