使用多个字段过滤/搜索 - ASP.NET MVC [英] Filter/Search using Multiple Fields - ASP.NET MVC
问题描述
我使用 ASP.NET MVC 与 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 that 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
以增强模型验证,并帮助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.
示例实施
假设您有一个产品
class:
Suppose you have a Product
class:
public class Product
{
public int Id { get; set; }
public int Price { get; set; }
public string Name { get; set; }
}
您可以创建一个 ProductSearchModel
class,并根据它们放置一些要搜索的字段:
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);
}
这篇关于使用多个字段过滤/搜索 - ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!