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 { set; get; }
  11. }
  12. }
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. namespace Mvctestlinq.Controllers
  8. {
  9. public class employeeeController : Controller
  10. {
  11. DataClasses1DataContext db = new DataClasses1DataContext();
  12. //
  13. // GET: /employeee/
  14. public ActionResult Index()
  15. {
  16. DataClasses1DataContext db = new DataClasses1DataContext();
  17. List<Employe> emps = db.Employes.ToList();
  18. return View(emps);
  19. }
  20. public ActionResult details(int id)
  21. {
  22. DataClasses1DataContext db = new DataClasses1DataContext();
  23. empcontext empss = new empcontext();
  24. empss.employess = db.Employes.ToList();
  25. var value = empss.employess.Single(d =>d.employeid == id);
  26. return View(value);
  27. }
  28. }
  29. }
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. ViewBag.Title = "Employe List";
  4. }
  5. <h2>Employe List</h2>
  6. <div>
  7. <ul>
  8. @foreach (var employe in @Model)
  9. {
  10. <li> @Html.ActionLink(employe.Name, "details", new { id = employe.employeid })</li>
  11. }
  12. </ul>
  13. </div>
  14. <
The following is the output of the preceding code:
Create a view for show details of Employe
  1. @model Mvctestlinq.Employe
  2. @{
  3. ViewBag.Title = "Details";
  4. }
  5. <h2>details</h2>
  6. <table>
  7. <tr>
  8. <td>
  9. <b>Employe Id</b>
  10. </td>
  11. <td>
  12. @Model.employeid
  13. </td>
  14. </tr>
  15. <tr>
  16. <td>
  17. <b>Name</b>
  18. </td>
  19. <td>
  20. @Model.Name
  21. </td>
  22. </tr>
  23. <tr>
  24. <td>
  25. <b>City</b>
  26. </td>
  27. <td>
  28. @Model.City
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <b>Gender</b>
  34. </td>
  35. <td>
  36. @Model.Gender
  37. </td>
  38. </tr>
  39. </tr>
  40. </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.