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")
-
11:19
Tundra Tactical
16 hours ago $0.15 earnedI Saw How CMMG Makes Guns.
5473 -
15:34
Misha Petrov
12 hours agoReacting To TikTok’s Most DELUSIONAL Takes!
1.29K4 -
1:52:24
Squaring The Circle, A Randall Carlson Podcast
1 day ago#032 Flournoy Holmes' Artwork Helped Define The Southern Rock Phenomenon of The Early 1970's
1.83K2 -
19:56
inspirePlay
1 day agoWalking with Lions & Facing Africa’s Wild Side | Safari Adventure with the Grid Championship Crew!
7 -
10:50
RTT: Guns & Gear
1 day agoBudget Friendly Carry 2011: EAA Girsan Brat 2311
2181 -
3:49:06
Alex Zedra
13 hours agoLIVE! New Game | Nuclear Nightmare
87.1K11 -
25:08
MYLUNCHBREAK CHANNEL PAGE
1 day agoUnder The Necropolis - Pt 2
270K61 -
1:45:59
Spittin' Chiclets
1 day agoCanadian Chokejob - Game Notes Live From Chicago - 12.28.2024
251K31 -
9:18
Space Ice
22 hours agoThe Guyver - Alien Bug Suits, Exploding Dragons, & Mark Hamill - Weirdest Movie Ever
159K9 -
9:31
Silver Dragons
1 day agoSilver Has Failed - Can it Set the Record Next Year? THIS BANK SAYS YES!
1.44K2