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
Drew Hernandez
10 hours agoLA MAYOR PUSHED $49 MILL LAFD BUDGET CUT ONE WEEK BEFORE FIRES?
3,317 watching -
2:52:04
Nobodies Gaming
6 hours ago $2.90 earnedNobodies Rumble Gaming TEST STREAM 2.0
35.9K3 -
1:00:36
Talk Nerdy 2 Us
5 hours agoDigital Surveillance, TikTok Shutdowns & The Hackers They Don’t Want You to Know About!
28.8K1 -
3:08:37
SpartakusLIVE
8 hours agoDelta Force || Tactical, Strategic, HARDCORE
42.5K2 -
3:32:05
I_Came_With_Fire_Podcast
11 hours agoTRUMP GUILTY Verdict, LA Fires, New American EXPANSIONISM, and Cyber Truck Updates!!
18K8 -
1:26:05
Glenn Greenwald
8 hours agoGOP Senators Demand Tulsi Support Domestic Surveillance To Be Confirmed; Group Tracks IDF War Criminals Around The World; System Pupdate: Pointer's Determination To Survive | SYSTEM UPDATE #387
91.7K58 -
57:27
Flyover Conservatives
1 day agoHealthy People Are Ungovernable: The Secrets They Don’t Want YOU to Know - Tracy Beanz | FOC Show
44.1K4 -
8:36:11
Dr Disrespect
15 hours ago🔴LIVE - DR DISRESPECT - DELTA FORCE - INTENSE SITUATIONS ONLY!
241K27 -
4:01:30
Nerdrotic
11 hours ago $33.42 earnedHollywood National DISASTER! Studios Terrified, Star Wars FAIL | Friday Night Tights 336 w Raz0rfist
139K43 -
2:52:10
Edge of Wonder
11 hours agoLA Fires: Biblical Inferno as Hollywood Burned Down
43.4K12