The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
What We are Going to cover
* In this tutorial, we are going to cover how to implement search functionality in ASP.NET MVC 4. 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.
Creating Database For This Application
- First create Database
* Open Microsoft SQL server. Click on New Query. Now execute the query below to create database.
Create Database searchingInmvc
- Then Press F5. This will create database successfully.
Create Table and Insert Demo Data
create table tblEmployee //creating table
ID int identity primary key,
//Inserting Demo Data
Here, we are inserting 4 Rows in Database table:
insert into tblEmployee values('John','Female','firstname.lastname@example.org')
insert into tblEmployee values('funky','Male','email@example.com')
insert into tblEmployee values('wiley','Male','firstname.lastname@example.org')
insert into tblEmployee values('ceren','Female','email@example.com')
Create New MVC 4 Application in Visual Studio
- Start Visual Studio with language selected as C#
- Click on File --> Then click on New Project
- Scroll down and select ASP.NET MVC 4 Web Application
- Give appropriate Name, Path and solution Name and Hit Enter
- Choose Empty Template
- Choose Razor View Engine and Hit Enter
- Now new MVC 4 web application is started.
Adding Models to MVC 4 Application
- In solution Explorer -- > Right click on Models --> Then click on New item
- Then select ADO.NET Entity Data Model --.> Give it a valid name like '
- Click Add:
- Now a dialog box appears, choose database connection to SQL Server and give your connection string a name that will be give to connection string in web.config file.
- Click next.
- Now in next dialog box, you will be presented with tables available in database table. Select your table.
- Now click Finish.
- Now Entity model of table is generated, you can rename your database here to '
- Now Model is successfully added.
Adding Controller and Views To MVC 4 Application
To add controller to database:
- Right click on Controllers folder in solution folder.
- Click on Add --> Then click on Controller:
- Now Add controller Dialog Box appears
- Give your controller a name like '
- In template, choose 'MVC controller with read/write and views using Entity Framework'
- The reason behind choosing this is it will automatically generate some pages to insert, delete, update data of model to which we are associating this controller
- Now give the Model name that we have added in previous step '
- Now choose
dbcontext class from dropdown menu and finally click ADD
- This automatically add Views for insert, update, delete and index view in views folder under controller named folder.
- Now run your application. This will give output below:
Adding Style And Look to Application
So we are going to search the database and retrieve data from database and then show the retrieved result to user. For this, we have to get some controls and code. The GUI design that we are going to give to this application is as below:
- In solution Explorer under Index view, Double click on Index.cshtml
- Now just before the line '
<h2>Index</h2>', add a line:
and close this div right below the code after table tag
Other updates are in demo project. Download and use it.
Update your Index.cshtml file under view folder in solution Explorer with the code in index.cshtml in uploaded file.
In the code in attached file, we have used Razor code and instead of using
<form> tag that we usually use in HTML, we have used HTML Helper below:
@using (Html.BeginForm ("Index","Home",FormMethod.Get ))
<b>Search by:</b>@Html.RadioButton("searchBy","Name")<text> Name</text>
@Html.TextBox("Search");<input type="submit" value="Search" />
In Html.BeginForm ("Index","Home",FormMethod.Get )
Html.BeginForm is used in place of using
Html.BeginForm received 3 arguments:
- Here, "
Index" is Name of action that will be executed on posting form to server
- Here, "
Home" is name of controller to which this link will be redirected when clicked
- Here, "
FormMethod.Get" type of encoding method that is applied on data we are posting
Now open Controller and add code below to Index Action of home controller:
if (searchby == "Gender")
return View(db.Employees.Where(x => x.Gender == search || search ==null).ToList());
return View(db.Employees.Where(x => x.Name.StartsWith(search)).ToList());