MVC 1.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前正在做类似的事情。
也就是说,我有一个 MVC 视图,其中包含各种搜索选项(复选框、下拉列表、文本框),并且我想要一种优雅的方式来返回“搜索结果”。
所以我创建了一个简单的类 - 例如“ProductSearchCriteria”。
该类只包含不同搜索选项的 getter/setter(当通过模型绑定提交表单时我会填充这些选项)。
然后,我接受此类型作为我的 BLL 方法的参数:
至于如何应用过滤器,这取决于一些事情。首先,我不知道您是否使用 Linq-To-Sql、NHibernate、Entity-Framework 等。这也取决于您的架构(存储库)。
您将无法通过 lambda 表达式“动态”应用过滤器(无论如何,这并不容易)。
我所做的是创建一个扩展方法来优雅地应用过滤器:
正如我所说,这取决于您的架构和 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:
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:
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.
您正在寻找的是一种构建
动态 LINQ 查询
的方法。您可以搜索有关它的一些详细信息以及那里的选项,但是,我相信 Scott Guthrie 编写的动态 Linq 库正是您正在寻找的。它允许您从字符串构建查询:
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: