Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement search features in ASP.NET MVC applications

I can imagine many ways of implemeting search features in an ASP.NET MVC application but since I can't find much documentation I was wondering if you have any common pattern, technology or common approach to implement search features in a ASP.NET MVC application (similar to stackoverflow). Some technologies that I have in mind are:

  • SQL Server full-text search
  • External search engine (like Search Server 2008)
  • Lucene.NET

...but what is the best approach to integrate them with ASP.NET MVC?

Ideas?

like image 882
sachaa Avatar asked Feb 18 '09 23:02

sachaa


People also ask

How can add search box in ASP NET MVC?

Right-click on the controller folder then select Add -> controller. Select MVC 5 Controller with read/write actions and click Add. Provide the controller a name. Click Add.

How can search by ID in MVC?

Right click on Index method and select Strong -typed view as shown in the following figure. In above code I have two html radiobutton and one html text box and simple submit Button. Press F5 and run you application. Here I have checked the name Radiobutton and searched name starting from S alphabet.

How to implement search functionality in MVC 5 razor?

The Search functionality will be implemented using a Stored Procedure which will be called by passing the parameter value using Entity Framework in ASP.Net MVC 5 Razor. Here I am making use of Microsoft’s Northwind Database.

How to implement search functionality using Entity Framework in MVC?

The Search functionality will be implemented using a Stored Procedure which will be called by passing the parameter value using Entity Framework in ASP.Net MVC 5 Razor. Here I am making use of Microsoft’s Northwind Database. You can download it from here. You will need to create the following Stored Procedure in the Northwind Database.

How to create MVC application in ASP NET?

Provide a meaningful name and click OK. Select an empty template and choose the type as MVC for the ASP.Net project. Once you click OK, a blank ASP.Net MVC application will be created.

How to search the database objects for specific data in MVC?

In our web applications in MVC, we often need to add the functionality to search the database objects for specific data based on some criteria like to find employees with name starting with 'N' or to find data of employees that have Gender Male. * Open Microsoft SQL server. Click on New Query. Now execute the query below to create database.


2 Answers

It's not entirely clear what you are specifically asking, but, in general:

  1. Write a view helper or partial view which returns a search form. Call that within your other pages wherever you need to display a search box. Make the form action GET, not POST.
  2. For a site search, you'll probably want to have a search controller. For searching within one particular type of data, you can add an action to an existing controller or an argument to an existing action. For the most part, the only thing that we have to add is an argument to the general-purpose "List" action for a specific datatype. The search form calls "List" and sets an argument with the search query string.
  3. The actual searching is done within your Repository. That's the only part of the application which knows about things like SQL Server or Lucene. For trivial cases a controller could append a .Where to an IQueryable<T> returned by a Repository.
like image 124
Craig Stuntz Avatar answered Oct 16 '22 13:10

Craig Stuntz


I believe in one of his blog posts Jeff Atwood talks about how he used sitemaps in order to let google handle most of the searching capabilities on stack overflow. Why write your own searching algorithms when people are likely just going to use google anyway?

like image 29
kgrad Avatar answered Oct 16 '22 13:10

kgrad