Generate Hyperlinks Using Actionlink Html Helper

Database structure

Create a table in a database for an Empoye table to store Employe information.
 
Let us see the create table code.
  1. CREATE TABLE [dbo].[Employe]  
  2. (  
  3. [employeid] [int] IDENTITY(1,1) NOT NULL,  
  4. [Name] [varchar](50) NOT NULL,  
  5. [Gender] [varchar](50) NOT NULL,  
  6. [City] [varchar](50) NOT NULL,  
  7. )  
Create MVC Application

Step 1
 
Go to File => New => Project.
Step 2
 
Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application the name "MVCtestlinq" and set the path in the location input where you want to create the application.
 
Step 3
 
Now choose the Project Template "Empty".
 
Adding a LINQ to SQL Class

Step 1
 
Right-click on the project then seelct Add new item then select Data from the templates.
 
Step 2
 
Choose "LINQ to SQL classes" from the list and provide the name. Then click on Add. After clicking Add we can see the .dbml file in our project.
 
Step 3
 
Drag the Employe table from the database in the Server Explorer.

 
Create Model Class

The MVC model contains all the application logic validation, Logic Business Logic and data access logic. We can create an employecontext class under the Model Folder.
  1. namespace Mvctestlinq.Models  
  2. {  
  3.     class empcontext  
  4.  {  
  5.         DataClasses1DataContext db = new DataClasses1DataContext();  
  6.         public empcontext()  
  7.         {  
  8.             this.employess = new List<Employe>();  
  9.         }     
  10.         public List<Employe> employess { setget; }   
  11.     }  
  12.       
  13. }  
Create controller class for Employe
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6. using Mvctestlinq.Models;    
  7.     
  8. namespace Mvctestlinq.Controllers    
  9. {    
  10.     public class employeeeController : Controller    
  11.     {    
  12.         DataClasses1DataContext db = new DataClasses1DataContext();    
  13.         //    
  14.         // GET: /employeee/    
  15.     
  16.         public ActionResult Index()    
  17.         {    
  18.             DataClasses1DataContext db = new DataClasses1DataContext();    
  19.            List<Employe> emps = db.Employes.ToList();    
  20.     
  21.             return View(emps);    
  22.     
  23.         }    
  24.         public ActionResult details(int id)    
  25.         {    
  26.             DataClasses1DataContext db = new DataClasses1DataContext();    
  27.     
  28.             empcontext empss = new empcontext();    
  29.             empss.employess = db.Employes.ToList();    
  30.             var value = empss.employess.Single(d =>d.employeid == id);    
  31.             return View(value);            
  32.     
  33.         }      
  34.     }    
  35. }    
Create a view to show the Employe Name

Right-click on Index Actionresult method and select Add view. After selecting add view, a dialog box will open. The view name is index by default and select your model class and click on the OK button.
 
Let's see each view with code.
  1. @model IEnumerable< Mvctestlinq.Employe>    
  2.     
  3. @{    
  4.     ViewBag.Title = "Employe List";    
  5. }    
  6.     
  7. <h2>Employe List</h2>    
  8.     
  9. <div>    
  10.     <ul>    
  11.         @foreach (var employe in @Model)    
  12.         {    
  13.             <li>  @Html.ActionLink(employe.Name, "details"new { id = employe.employeid })</li>    
  14.         }    
  15.     </ul>    
  16. </div>    
  17. <    
The following is the output of the preceding code:
 
 
Create a view for show details of Employe
  1. @model Mvctestlinq.Employe  
  2.   
  3. @{  
  4.     ViewBag.Title = "Details";  
  5. }  
  6.   
  7. <h2>details</h2>  
  8.   
  9. <table>  
  10.     <tr>  
  11.         <td>  
  12.             <b>Employe Id</b>  
  13.         </td>  
  14.         <td>  
  15.             @Model.employeid  
  16.         </td>  
  17.     </tr>  
  18.     <tr>  
  19.         <td>  
  20.             <b>Name</b>  
  21.         </td>  
  22.         <td>  
  23.             @Model.Name  
  24.         </td>  
  25.     </tr>  
  26.     <tr>  
  27.         <td>  
  28.             <b>City</b>  
  29.         </td>  
  30.         <td>  
  31.             @Model.City  
  32.         </td>  
  33.     </tr>  
  34.     <tr>  
  35.         <td>  
  36.             <b>Gender</b>  
  37.         </td>  
  38.         <td>  
  39.             @Model.Gender  
  40.         </td>  
  41.     </tr>  
  42.       
  43.     </tr>  
  44. </table>  
 
If we will click on any name of the Employe then the page will redirect to the detail page due to Hyperlinks using an Actionlink html helper.
 
 
 
Summary

In this article we learned about Hyperlinks using Actionlink html helper using LINQ to SQL.

Up Next
    Ebook Download
    View all
    Learn
    View all