MVC 1.0 中的动态搜索选项

发布于 2024-10-07 07:57:44 字数 555 浏览 2 评论 0原文

我正在寻找一种动态方法来在我的 MVC 1.0 应用程序中实现搜索。

假设我有一个包含文本框、下拉列表和按钮的用户控件。用户将在文本框中输入查询,在下拉列表中选择要搜索的列,然后按搜索按钮。

在执行上述活动时,我想在模型中执行此操作:

context.MyViewOrTableName.Where(p => (p.ColumnNameFromTheDropdown.Contains(DataFromTheTextbox)));

上述场景在 MVC 1.0 中是否可能,如果可以,那么如何实现? 任何帮助将不胜感激。

解决方案:

context.MyViewOrTableName.Where("" + ColumnNameFromTheDropdown + ".Contains(@0)", DataFromTheTextbox);

仅在包含 Scott 创建并由 Omar 在下面的帖子中提到的命名空间 System.Linq.Dynamic 后才会发生这种情况。

I'm looking for a dynamic way to implement search in my MVC 1.0 application.

Let's say that I have a user control containing a textbox, a dropdown and a button. The user will put a query in the textbox, select the column from which to search in the dropdown and then press the search button.

On doing the above activity I want to do this in the model:

context.MyViewOrTableName.Where(p => (p.ColumnNameFromTheDropdown.Contains(DataFromTheTextbox)));

Whether the above scenario is possible in MVC 1.0 and if so then how?
Any help would be appreciated.

Solution:

context.MyViewOrTableName.Where("" + ColumnNameFromTheDropdown + ".Contains(@0)", DataFromTheTextbox);

This happened only after including the namespace System.Linq.Dynamic created by Scott and referred by Omar in the post below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拿命拼未来 2024-10-14 07:57:45

我目前正在做类似的事情。

也就是说,我有一个 MVC 视图,其中包含各种搜索选项(复选框、下拉列表、文本框),并且我想要一种优雅的方式来返回“搜索结果”。

所以我创建了一个简单的类 - 例如“ProductSearchCriteria”。

该类只包含不同搜索选项的 getter/setter(当通过模型绑定提交表单时我会填充这些选项)。

然后,我接受此类型作为我的 BLL 方法的参数:

public ICollection<Product> FindProductsForCriteria(ProductSearchCriteria criteria)
{
   return _repository // GenericRepository<Product>
      .Find() // IQueryable<Product>
      .WithSearchCriteria(criteria) // IQueryable<Product>
      .ToList(); // List<Product>
}

至于如何应用过滤器,这取决于一些事情。首先,我不知道您是否使用 Linq-To-Sql、NHibernate、Entity-Framework 等。这也取决于您的架构(存储库)。

您将无法通过 lambda 表达式“动态”应用过滤器(无论如何,这并不容易)。

我所做的是创建一个扩展方法来优雅地应用过滤器:

public static IQueryable<Product> WithSearchCriteria(this IQueryable<Product> source, ProductSearchCriteria criteria)
{
    var query = source;

   if (criteria.SearchFilterOne != null)
       query = query.Where(x => x.FieldInModel == criteria.SearchFilterOne);
   // inspect other criteria
}

正如我所说,这取决于您的架构和 ORM。我使用实体框架 4.0,它支持延迟执行,这意味着我可以在对象上构建查询 (IQueryable),并在执行查询之前应用过滤器。

I'm currently doing a similar thing.

That is, I have an MVC View which contains various search options (checkbox, dropdown, textbox), and I wanted an elegant way to return "search results".

So I created a simple class - e.g "ProductSearchCriteria".

This class contains nothing but getters/setters for the different search options (which I populate when the form is submitted via model binding).

I then accept this type as a parameter on my BLL method:

public ICollection<Product> FindProductsForCriteria(ProductSearchCriteria criteria)
{
   return _repository // GenericRepository<Product>
      .Find() // IQueryable<Product>
      .WithSearchCriteria(criteria) // IQueryable<Product>
      .ToList(); // List<Product>
}

As to how to apply the filters, well that depends on a few things. Firstly, I don't know if your using Linq-To-Sql, NHibernate, Entity-Framework, etc. Also it depends on your architecture (repository).

You're not going to be able to "dynamically" apply filters via lambda expressions (not easily, anyway).

What I did was create an extension method to gracefully apply the filters:

public static IQueryable<Product> WithSearchCriteria(this IQueryable<Product> source, ProductSearchCriteria criteria)
{
    var query = source;

   if (criteria.SearchFilterOne != null)
       query = query.Where(x => x.FieldInModel == criteria.SearchFilterOne);
   // inspect other criteria
}

As I said, it depends on your architecture and ORM. I use Entity Framework 4.0, which supports deferred execution, meaning I can build up queries on my objects (IQueryable), and apply filters before executing the query.

场罚期间 2024-10-14 07:57:45

您正在寻找的是一种构建动态 LINQ 查询的方法。您可以搜索有关它的一些详细信息以及那里的选项,但是,我相信 Scott Guthrie 编写的动态 Linq正是您正在寻找的。

它允许您从字符串构建查询:

var query =
    db.Customers.
    Where("City = @0 and Orders.Count >= @1", "London", 10).
    OrderBy("CompanyName").
    Select("new(CompanyName as Name, Phone)");

What you're looking for is a way to build dynamic LINQ queries. You can search for some details about it and the options out there, however, I believe that the Dynamic Linq library that Scott Guthrie wrote is exactly what you're looking for.

It lets you build queries from strings:

var query =
    db.Customers.
    Where("City = @0 and Orders.Count >= @1", "London", 10).
    OrderBy("CompanyName").
    Select("new(CompanyName as Name, Phone)");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文