In this article I will explain with an example, how to call JavaScript function on Html.ActionLink click in ASP.Net MVC Razor.
When the ActionLink inside the WebGrid row is clicked, a JavaScript function will be called inside which the data from the WebGrid row will be fetched and displayed using JavaScript Alert Message Box.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
Call JavaScript function on Html.ActionLink click in ASP.Net MVC
 
 
Controller
The Controller consists of the following Action method.
Action method for handling GET operation of Index View
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Customer records are fetched using Entity Framework and returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        NorthwindEntities entities = new NorthwindEntities();
        return View(entities.Customers.ToList());
    }
}
 
 
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
The WebGrid is initialized with the Model i.e. IEnumerable collection of Customer Entity class objects as source.
The WebGrid is created using the GetHtml method with the following parameters.
HtmlAttributes – It is used to set the HTML attributes to the HTML Table generated by WebGrid such as ID, Name, Class, etc.
Columns – It is used to specify the columns to be displayed in WebGrid and also allows to set specific Header Text for the columns.
The last column of the WebGrid consists of an ActionLink created using Html.ActionLink Helper method.
The ActionLink has been specified with a JavaScript onclick event handler which makes call to the GetSelectedRow JavaScript function.
Inside the GetSelectedRow JavaScript function, the reference of the HTML Table row is determined and then using this reference, the data from each cell is fetched and displayed using JavaScript Alert Message Box.
@model IEnumerable<Customer>
@{
    Layout = null;
    WebGrid webGrid = new WebGrid(source: Model, canSort: false);
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Grid
        {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
        .Grid th
        {
            background-color: #F7F7F7;
            font-weight: bold;
        }
        .Grid th, .Grid td
        {
            padding: 5px;
            border: 1px solid #ccc;
        }
        .Grid, .Grid table td
        {
            border: 0px solid #ccc;
        }
         .Grid a, .Grid a:visited
        {
            color:blue;
        }
    </style>
</head>
<body>
    @webGrid.GetHtml(
        htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
        columns: webGrid.Columns(
                 webGrid.Column("CustomerID", "Customer Id"),
                 webGrid.Column("ContactName", "Customer Name"),
                 webGrid.Column("City", "City"),
                 webGrid.Column("Country", "Country"),
                 webGrid.Column(null, null, format: @<text>@Html.ActionLink("Select", null, null, new { @onclick = "return GetSelectedRow(this);" })</text>)
      ))
    <script type="text/javascript">
        function GetSelectedRow(link) {
            var row = link.parentNode.parentNode;
            var message = "Selected Row:";
            message += "\n\nCustomer Id: " + row.getElementsByTagName("TD")[0].innerHTML;
            message += "\nContact Name: " + row.getElementsByTagName("TD")[1].innerHTML;
            message += "\nCity: " + row.getElementsByTagName("TD")[2].innerHTML;
            message += "\nCountry: " + row.getElementsByTagName("TD")[3].innerHTML;
            alert(message);
            return false;
        }
    </script>
</body>
</html>
 
 
Screenshot
Call JavaScript function on Html.ActionLink click in ASP.Net MVC
 
 
Browser Compatibility
The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads