Action HTML Helpers In MVC

Overview

In the previous article, we saw how to access data from a database. Here, in this article, we are going to see what are the action HTML helpers in MVC and how to access those in our Views. A view will have a hyperlink and it will route to another view which contains data. So let's see how we use HTML helpers in MVC. We will be using the same example, here.

Introduction

First, let's add an Action Method, called Index, in our main controller i.e Employee.

code

Add this code to your controller, as we will be using the same object which we used in our previous action method.

Here, we will be retrieving the list of employees and passing them in View. So, List<Employee>.

Now, create a View. Just right click on Action Method and create a View,  a strongly typed View.

view

view

Click on Add. It will create a View inside Employee folder .

View

Now, open index.cshtml.

Now, just write the following lines in the file at top :

code

We are using IE numerable interface to return list of strings and also we had added namespace models.

Now, we will create <ul> tag first and write foreach statement, basically to loop records.

code
So, in foreach, we will be writing Employee which is our class file name in employees, here in our model. 

Now, just type in foreach loop.

code

@html.Actionlink

Now, look at what the IntelliSense says:

code

String linkname, actionname. Now, we will write:
code
As we are passing Empname, and Details is our controller and what we have to pass, create a new object and just pass id to it. Now, our final code is,

code
Now, run the solution and see what output we get.

Just type this URL for calling the Index action method,

URL

Here is our output:

output

Just click on Akshay; it will route to details action method and will display details,

output

As you can see in the above URL also, Details Action method has been called on click of Akshay with the help of specifying in the HTML action link .

Conclusion

So, this was about HTML actionlinks in MVC . I hope this article was helpful.

Up Next
    Ebook Download
    View all
    Learn
    View all