使用多个字段过滤/搜索 - ASP.NET MVC [英] Filter/Search using Multiple Fields - ASP.NET MVC
问题描述
我将 ASP.NET MVC 与 EF 6 一起使用.
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.
在下图中,我有 3 个选项.我可能会按每个选项进行过滤,一次一个或两个或三个的组合.
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.
我正在考虑为每个选择的选项编写 linq 查询.但是如果过滤器选项增加,这是不可能的.有没有更好的方法.
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.
谢谢!
这是我在我的控制器中所做的.(目前下拉列表有两个选项,不包括:--选择一个--")
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 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);
}
优点:
- 您可以根据自己的要求在
ProductSearchModel
中放入任何您需要的东西. - 您可以根据需求在
GetProducts
中编写任何逻辑.没有限制. - 如果您添加新的字段或选项进行搜索,您的操作和控制器将保持不变.
- 如果您的搜索逻辑发生变化,您的操作和控制器将保持不变.
- 您可以在需要搜索产品、控制器甚至其他业务逻辑的任何地方重复使用搜索逻辑.
- 有了这样的
ProductSearchModel
,你可以将它用作ProductSearch
局部视图的模型,你可以将DataAnnotations
应用到它来增强模型验证并使用Display
或其他属性帮助 UI 呈现它. - 您可以在该业务逻辑类中添加与您的产品相关的其他业务逻辑.
- 按照这种方式,您可以拥有一个更有条理的应用程序.
- 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 ofProductSearch
partial view and you can applyDataAnnotations
to it to enhance the model validation and help UI to render it usingDisplay
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.
示例实现:
假设您有一个 Product
类:
public class Product
{
public int Id { get; set; }
public int Price { get; set; }
public string Name { get; set; }
}
您可以创建一个 ProductSearchModel
类并根据它们放置一些您要搜索的字段:
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; }
}
然后你可以把你的搜索逻辑放在 ProductBusinessLogic
类中:
Then you can put your search logic in ProductBusinessLogic
class this way:
public class ProductBusinessLogic
{
private YourDbContext Context;
public ProductBusinessLogic()
{
Context = new YourDbContext();
}
public IQueryable<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;
}
}
然后在你的 ProductController
中你可以这样使用:
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);
}
重要提示:
在现实世界的实现中,请考虑为您的业务类实现合适的 Dispose
模式,以便在需要时处理数据库上下文.有关更多信息,请查看 实施 Dispose 方法 或 处置模式.
In a real world implementation, please consider implementing a suitable Dispose
pattern for your business class to dispose db context when needed. For more information take a look at Implementing a Dispose method or Dispose Pattern.
这篇关于使用多个字段过滤/搜索 - ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!