Part 62 Implementing search functionality in asp net mvc

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

We will be using table tblEmployee for this demo. Use the script below to create and populate the table with sample data.
Create table tblEmployee
(
ID int identity primary key,
Name nvarchar(50),
Gender nvarchar(10),
Email nvarchar(50)
)

Step 2: Generate ADO.NET entity data model from database using table tblEmployee. Change the entity name from tblEmployee to Employee. Save changes and build the application.

Step 3: Add HomeController with the following settings.
a) Controller name = HomeController
b) Template = MVC controller with read/write actions and views, using Entity Framework
c) Model class = Employee
d) Data context class = SampleDBContext
e) Views = Razor

Step 4: Modify the Index() action method in HomeController as shown below.
public ActionResult Index(string searchBy, string search)
{
if (searchBy == "Gender")
{
return View(db.Employees.Where(x =] x.Gender == search || search == null).ToList());
}
else
{
return View(db.Employees.Where(x =] x.Name.StartsWith(search) || search == null).ToList());
}
}

@model IEnumerable[MVCDemo.Models.Employee]

@{
ViewBag.Title = "Index";
}
[div style="font-family:Arial"]
[h2]Employee List[/h2]
[p]
{
[b]Search By:[/b]
@Html.RadioButton("searchBy", "Name", true) [text]Name[/text]
@Html.RadioButton("searchBy", "Gender") [text]Gender[/text][br /]
@Html.TextBox("search") [input type="submit" value="search" /]

}
[/p]
[table border="1"]
[tr]
[th]
@Html.DisplayNameFor(model =] model.Name)
[/th]
[th]
@Html.DisplayNameFor(model =] model.Gender)
[/th]
[th]
@Html.DisplayNameFor(model =] model.Email)
[/th]
[th]Action[/th]
[/tr]
@if (Model.Count() == 0)
{
[tr]
[td colspan="4"]
No records match search criteria
[/td]
[/tr]
}
else
{
foreach (var item in Model)
{
[tr]
[td]
@Html.DisplayFor(modelItem =] item.Name)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.Gender)
[/td]
[td]
@Html.DisplayFor(modelItem =] item.Email)
[/td]
[td]
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
[/td]
[/tr]
}
}
[/table]
[/div]

Please make sure to replace [ with LESSTHAN and ] with GREATERTHAN symbol.
Рекомендации по теме
Комментарии
Автор

We searched 25 articles for this simple example you are the only one to give a simple, clear, and concise answer on this basic functionality! Thank you Sir!

abundancearchitect
Автор

wow! I have never taken a course with such simplicity and all the required steps! Well done.

natr
Автор

Hello sir, I would like to appreciate your work in preparing these tutorials. As a beginner, I went through these videos and I could get every sort of help in learning asp.net mvc4. I strongly recommend these videos for all those mvc4 beginners!!!

ippiliharish
Автор

Is it possible that Venkat is my favourite teacher on Absolutely

mikehulme
Автор

Probably Venkat can be regarded as Lord of .Net Development on Youtube!

sharathpanchagnula
Автор

Kudos Venkat! Your videos are the most concise, understandable and easy to follow tutorials I have come across. Thank you for your all the time and effort you made.

KenKraska
Автор

The thing I like about these tutorials is that Venkat explains only what you need to know to understand the topic at hand - he doesn't introduce a lot of extraneous fluff which, although valid, does not help with understanding the topic. If you've ever tried to follow a Microsoft tutorial, you'll understand what I mean. It's like Mark Twain once quipped, "Intellectual food is like any other - it's easier taken with a spoon than with a shovel."

theoldcrab
Автор

seriously thank you for your videos
You really helped me been trying to do this for so long so many tutorials were so complicated
THANK YOU keep it up

juliusmaka
Автор

Venkat, thank you so much for making my life easy. You are the best tutor.

amritgopali
Автор

Include a reference to site.css stylesheet, and see if that helps. You can get this stylesheet, if you create a new asp.net mvc4 application using Internet Template. Hope this will the issue you are facing.

Csharp-video-tutorialsBlogspot
Автор

you can also search only part of names or gender by using this code

arianitonline
Автор

Hello, many thanks for all your videos.
I also would like to appreciate your work it's really a great job and help everyone who is learning to programming.
Best Regards

Vlvs
Автор

hai sir ...i have seen your vedios..those are very very helpul to me.. thanx a

ambicadevi
Автор

Love your videos Venkat! Thank you for all the help!

pneiman
Автор

Thank you so much for making this video! It REALLY helped me!

rachel
Автор

Nice work help me to understand the search etc....
Thank you

dumitrurobert
Автор

your vid have saved a week of my life
REALLY THANK YOU !!!
*BIG LIKE*

LightMinz
Автор

Thank you thousand times and God bless

dancunchiriga
Автор

Thank you very much you helped me a lot

irmagalicia
Автор

Thank you man for your video!!!! you helped so much!!!! Congratttzzzz

MateusMarquezini