如何在MVC中创建搜索框 [英] How to create search box in MVC

查看:195
本文介绍了如何在MVC中创建搜索框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





任何人都可以帮我创建一个带有四个文本框的搜索模块



模块详情:



4文本框

1搜索按钮。

表:员工

表字段:ID,名称,位置,移动,薪水和指定。



功能:

用户可以使用文本框搜索Emplaoyee详细信息,填写1个文本框字段是必填项,他也可以输入2个字段,3个字段或4个字段取决于过滤。





任何人都可以帮助我。在使用SQL Server作为后端数据库的MVC代码中



我尝试过:



任何人都可以帮助我。在使用SQL Server作为后端数据库的MVC代码中

解决方案

尽管ASP.NET MVC实现并充分利用了Entity框架,但我建议使用LINQ查询。例如,

  var  people = dbContext.GetPeople(); 
var person =( from p in people
其中 p.Name == requiredName
选择 p) .ToList();

// 其他操作。



如果情况并非如此,并且您想要实现自己的搜索,那么我的这篇文章可能对您有帮助,如何使用ASP.NET在数据库内搜索相关查询 [ ^ ]。这需要一个SQL语句来搜索相关的术语,

  //   ASP.NET  
var requiredName =


%{query}%;
var sqlStatement = SELECT * FROM人员在哪里[名称] LIKE @ 0;
var result = db.Query(sqlStatement,requiredName);



ASP.NET方式这样做有点不同,这段代码实现了SQL注入预防方法。如果要在 SqlClient 命名空间中使用对象,请考虑 SqlParameter 对象。



System.Data.SqlClient命名空间 [ ^ ]

LINQ(语言集成查询) ) [ ^ ]


嗯......我建议首先阅读以下文章:

排序,过滤,以及在ASP.NET MVC应用程序中使用实体框架进行分页ASP.NET站点 [ ^ ]

< a href =http://www.asp.net/mvc/overview/getting-started/introduction/adding-search>添加搜索方法和搜索视图 [ ^ ]

在ASP.NET MVC应用程序中实现搜索功能 [ ^ ]

Hi,

Can anyone help me to create an Search module with four textbox

Module Details :

4 Textbox
1 Search Button.
Table : Employee
Table Fields : ID , Name , Location, Mobile , Salary and Designation.

Functionality :
User can Search the Emplaoyee Details by using the Textbox,1 Text box field to fill is mandatory and he can also enter 2 fields ,3 fields or 4 fields depends on the filtration.


Can anyone help me . in MVC code using SQL Server as Backend DB

What I have tried:

Can anyone help me . in MVC code using SQL Server as Backend DB

解决方案

Although ASP.NET MVC implements and makes a good use of Entity framework, for that I would suggest using LINQ queries. Such as,

var people = dbContext.GetPeople();
var person = (from p in people
              where p.Name == requiredName
              select p).ToList();

// Other operations.


If that is not the case and you want to implement your own search, then this article of mine might help you, How to search for related query inside the Database using ASP.NET[^]. That requires an SQL statement to search for the related terms,

// ASP.NET
var requiredName =


"%{query}%"; var sqlStatement = "SELECT * FROM Persons WHERE [Name] LIKE @0"; var result = db.Query(sqlStatement, requiredName);


The ASP.NET way of doing this is a bit different, and this code has SQL Injection prevention method implemented. If you want to use objects in SqlClient namespace, then consider the SqlParameter objects.

System.Data.SqlClient Namespace[^]
LINQ (Language-Integrated Query)[^]


Well... i'd suggest to start by reading below articles:
Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application | The ASP.NET Site[^]
Adding a Search Method and Search View[^]
Implement Search Functionality into your ASP.NET MVC Application[^]


这篇关于如何在MVC中创建搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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