Premium Only Content
Part 9 Generate hyperlinks using actionlink html helper
In this video we will discuss, generating hyperlinks using actionlink html helper, for navigation between mvc pages.
Please watch Part 8, before proceeding.
We want to display all the employees in a bulletted list as shown below. Notice that all the employee names are rendered as hyperlinks.
Adding Links between pages using html helpers.png
When the hyperlink is clicked, the user will be redirected to employee details page, displaying the full details of the employee as shown below.
Generate links using Html.ActionLink html helper.png
Copy and paste the following Index() action method in EmployeeController class. This method retrieves the list of employees, which is then passed on to the view for rendering.
public ActionResult Index()
{
EmployeeContext employeeContext = new EmployeeContext();
// Replace square brackets with angular brackets
List[Employee] employees = employeeContext.Employees.ToList();
return View(employees);
}
At the moment, we don't have a view that can display the list of employees. To add the view
1. Right click on the Index() action method
2. Set
View name = Index
View engine = Razor
Select, Create a stronlgy-typed view checkbox
Select "Employee" from "Model class" dropdownlist
3. Click Add
At the point, "Index.cshtml" view should be generated. Copy and paste the following code in "Index.cshtml".
@* Replace square brackets with angular brackets *@
@model IEnumerable[MVCDemo.Models.Employee]
@using MVCDemo.Models;
[div style="font-family:Arial"]
@{
ViewBag.Title = "Employee List";
}
[h2]Employee List[/h2]
[ul]
@foreach (Employee employee in @Model)
{
[li]@Html.ActionLink(employee.Name, "Details", new { id = employee.EmployeeId })[/li]
}
[/ul]
[/div]
Points to Remember:
1. @model is set to IEnumerable[MVCDemo.Models.Employee]
2. We are using Html.ActionLink html helper to generate links
Copy and paste the following code in Details.cshtml
@Html.ActionLink("Back to List", "Index")
-
LIVE
Fresh and Fit
1 hour agoAfter Hours w/ Girls
6,283 watching -
LIVE
CHiLi XDD
2 hours ago[F EM UP Friday] Take # 2 [Destiny 2] Lets Kick Some A$$! #RumbleTakeOver
1,202 watching -
ItsMossy
8 hours agoHALO WITH THE RUMBLERS (: #RUMBLETAKEOVER
2261 -
DVR
INFILTRATION85
2 hours agoHi, I'm INFILTRATION
11K6 -
LIVE
GuardianRUBY
3 hours agoRumble Takeover! The Rumblings are strong
993 watching -
LIVE
Etheraeon
11 hours agoWorld of Warcraft: Classic | Fresh Level 1 Druid | 500 Follower Goal
559 watching -
LIVE
VapinGamers
3 hours ago $1.47 earned🎮🔥Scrollin’ and Trollin’: ESO Adventures Unleashed!
335 watching -
LIVE
a12cat34dog
4 hours agoGETTING AFTERMATH COMPLETED :: Call of Duty: Black Ops 6 :: ZOMBIES CAMO GRIND w/Bubba {18+}
168 watching -
LIVE
NubesALot
7 hours ago $1.13 earnedDark Souls Remastered and party games
486 watching -
3:03:42
GamersErr0r
18 hours ago $0.64 earnedits not what you think
9.35K1