使用过滤器/搜索多个字段 - ASP.NET MVC [英] Filter/Search using Multiple Fields - ASP.NET MVC

查看:88
本文介绍了使用过滤器/搜索多个字段 - ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 ASP.NET MVC EF 6

我有一只股票网页,其中显示了股票项目的所有信息。现在我要筛选记录了。

在图片下面我有3个选项。我可能由每个选项,一次一个或两个组合或与所有三个过滤。

我的想法写LINQ查询为每选择的每个选项。但是,如果过滤器选项increases.Is有这个没有更好的方法,这将是不可能的。

谢谢!

在这里输入的形象描述

这是我在我的控制器做(目前下拉菜单有两个选项,但不包括: - 选择一个 - )。

 公众的ActionResult StockLevel(字符串选项,字符串批,字符串名称)
{
    如果(选项=0&放大器;!&安培;一批==&放大器;&放大器;名称==)
    {
        如果(选项==BelowMin)
        {
            清单<股票及GT; STK =(从s在db.Stocks
                               其中,s.Qty< s.Item.AlertQty
                               选择S).ToList();
            返回查看(STK);
        }
        其他
        {
            清单<股票及GT; STK =(从s在db.Stocks
                               其中,s.Qty == s.InitialQty
                               选择S).ToList();
            返回查看(STK);
        }
    }
    如果(选项==0&放大器;&安培;!批量=与&&放大器;名称==)
    {
        清单<股票及GT; STK =(从s在db.Stocks
                           其中,s.BatchNo ==批
                           选择S).ToList();
        返回查看(STK);
    }
    如果(选项==0&放大器;&安培;一批==&放大器;&安培; NAME =!)
    {
        清单<股票及GT; STK =(从s在db.Stocks
                           其中,s.Item.Name.StartsWith(+姓名+)
                           选择S).ToList();
        返回查看(STK);
    }
    返回查看(db.Stocks.ToList());
}


解决方案

我建议你分开的关注和使用方法,在控制器的code是这样的,简洁,美观,可扩展的:

 公众的ActionResult指数(ProductSearchModel searchModel)
{
    VAR业务=新ProductBusinessLogic();
    VAR模型= business.GetProducts(searchModel);
    返回查看(模型);
}

好处:


  • 您可以把任何你需要你的 ProductSearchModel 根据您的要求。

  • 您可以写在根据要求的GetProducts 的任何逻辑。没有任何限制。

  • 如果您添加一个新的领域或选项进行搜索,你的动作和控制器将保持不变。

  • 如果您的搜索变化的逻辑,你的行动,控制器将保持不变。

  • 无论您需要在产品搜索,在控制器甚至在其他的业务逻辑可以重用的搜索逻辑。

  • 有这样 ProductSearchModel ,你可以使用它作为产品搜索局部视图的模型,你可以申请 DataAnnotations 将其提升模型验证,并帮助用户界面使用显示或其他属性来呈现它。

  • 您可以添加在业务逻辑类相关产品的其他业务逻辑。

  • 以下这种方式,你可以有一个更有组织的应用程序。

样品注入:

假设你有一个产品类:

 公共类产品
{
    公众诠释标识{搞定;组; }
    公众诠释价格{搞定;组; }
    公共字符串名称{;组; }
}

您可以创建一个 ProductSearchModel 类,并把您想要搜索基于它们的一些字段:

 公共类ProductSearchModel
{
    公众诠释?标识{搞定;组; }
    公众诠释?包含pricefrom {搞定;组; }
    公众诠释? PriceTo {搞定;组; }
    公共字符串名称{;组; }
}

然后你可以把你的搜索逻辑在 ProductBusinessLogic 类是这样的:

 公共类ProductBusinessLogic
{
    私人YourDbContext语境;
    公共ProductBusinessLogic()
    {
        上下文=新YourDbContext();
    }    公共IQeryable<产品与GT;的GetProducts(ProductSearchModel searchModel)
    {
        VAR的结果= Context.Products.AsQueryable();
        如果(searchModel!= NULL)
        {
            如果(searchModel.Id.HasValue)
                结果= result.Where(X => x.Id == searchModel.Id)​​;
            如果(!string.IsNullOrEmpty(searchModel.Name))
                结果= result.Where(X => x.Name.Contains(searchModel.Name));
            如果(searchModel.PriceFrom.HasValue)
                结果= result.Where(X => x.Price> = searchModel.PriceFrom);
            如果(searchModel.PriceTo.HasValue)
                结果= result.Where(X => x.Price< = searchModel.PriceTo);
        }
        返回结果;
    }
}

然后在你的 ProductController的你可以用这种方式:

 公众的ActionResult指数(ProductSearchModel searchModel)
{
    VAR业务=新ProductBusinessLogic();
    VAR模型= business.GetProducts(searchModel);
    返回查看(模型);
}

I am using ASP.NET MVC with EF 6.

I have a stock page which shows all the information on stock items. Now I want to filter records too.

In picture below I have 3 options. I might filter by each option, one at a time or by combination of two or with all three.

I was thinking of writing linq query for each and every options selected. But this wouldn't be possible if filter option increases.Is there is any better way to this.

Thanks!

This is what I did in my controller.(currently dropdown has two options, excluding : " -- select one -- ")

public ActionResult StockLevel(string option, string batch, string name)
{
    if (option != "0" && batch == "" && name == "")
    {
        if(option == "BelowMin")
        {
            List<Stock> stk = (from s in db.Stocks
                               where s.Qty < s.Item.AlertQty
                               select s).ToList();
            return View(stk);
        }
        else
        {
            List<Stock> stk = (from s in db.Stocks
                               where s.Qty == s.InitialQty
                               select s).ToList();
            return View(stk);
        }
    }
    if (option == "0" && batch != "" && name == "")
    {
        List<Stock> stk = (from s in db.Stocks
                           where s.BatchNo == batch
                           select s).ToList();
        return View(stk);
    }
    if (option == "0" && batch == "" && name != "")
    {
        List<Stock> stk = (from s in db.Stocks
                           where s.Item.Name.StartsWith(""+name+"")
                           select s).ToList();
        return View(stk);
    }
    return View(db.Stocks.ToList());
}

解决方案

I recommend you to separate concerns and use an approach that the code in your controller be like this, simple, beautiful and extensible:

public ActionResult Index(ProductSearchModel searchModel)
{
    var business = new ProductBusinessLogic();
    var model = business.GetProducts(searchModel);
    return View(model);
}

Benefits:

  • You can put anything you need in your ProductSearchModel based on your requirements.
  • You can write any logic in GetProducts based on requirements. There is no limitation.
  • If you add a new field or option to search, your action and controller will remain untouched.
  • If the logic of your search changes, your action and controller will remain untouched.
  • You can reuse logic of search wherever you need to search on products, in controllers or even in other business logic.
  • Having such ProductSearchModel, you can use it as model of ProductSearch partial view and you can apply DataAnnotations to it to enhance the model validation and help UI to render it using Display or other attributes.
  • You can add other business logic related to your product in that business logic class.
  • Following this way you can have a more organized application.

Sample Implantation:

Suppose you have a Product class:

public class Product
{
    public int Id { get; set; }
    public int Price { get; set; }
    public string Name { get; set; }
}

You can create a ProductSearchModel class and put some fields you want to search based on them:

public class ProductSearchModel
{
    public int? Id { get; set; }
    public int? PriceFrom { get; set; }
    public int? PriceTo { get; set; }
    public string Name { get; set; }
}

Then you can put your search logic in ProductBusinessLogic class this way:

public class ProductBusinessLogic
{
    private YourDbContext Context;
    public ProductBusinessLogic()
    {
        Context = new YourDbContext();
    }

    public IQeryable<Product> GetProducts(ProductSearchModel searchModel)
    {
        var result = Context.Products.AsQueryable();
        if (searchModel != null)
        {
            if (searchModel.Id.HasValue)
                result = result.Where(x => x.Id == searchModel.Id);
            if (!string.IsNullOrEmpty(searchModel.Name))
                result = result.Where(x => x.Name.Contains(searchModel.Name));
            if (searchModel.PriceFrom.HasValue)
                result = result.Where(x => x.Price >= searchModel.PriceFrom);
            if (searchModel.PriceTo.HasValue)
                result = result.Where(x => x.Price <= searchModel.PriceTo);
        }
        return result;     
    }
}

Then in your ProductController you can use this way:

public ActionResult Index(ProductSearchModel searchModel)
{
    var business = new ProductBusinessLogic();
    var model = business.GetProducts(searchModel);
    return View(model);
}

这篇关于使用过滤器/搜索多个字段 - ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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