How to generate HyperLink In MVC?
In this article we will learn How to generate HyperLink In MVC(Asp.Net MVC) by Sagar Jaybhay.
For this, we need to use Html.ActionLink helper function is used to
generate an action link. To understand this better we get first 100 employee
list where contain only EmployeeID hyperlink after clicking on this link you
will able to access all the details about that EmmployeeID.
For this, we create one action method in our EmployeeController which return a list of ID view and If you click on that you will be redirected to Index view which takes one parameter EmpID and displays that employee Detail.
First, we write the business logic method to get top 100 EmpIds.
public List<int> GetEmpIDs()
List<int> ids = new List<int>();
string Query = "select top 100 EmpID from Employee";
var data=this.dataAccess.GetTable(Query);
if(data!=null&&data.Rows.Count>0)
foreach (DataRow id in data.Rows)
ids.Add(Convert.ToInt32(id["EmpID"]));
return ids;
After this, we call this method from our newly created EmployeeController method.
public ActionResult GetEmpIDs()
var list = new BusinessLogic.Business().GetEmpIDs();
ViewData["ids"] = list;
return View();
Now we are writing a view and View code is below
@
ViewBag.Title = "GetEmpIDs";
<h2>GetEmpIDs</h2>
<h3>Here We can display ids</h3>
<ul>
@foreach (var id in (List<int>)ViewData["ids"])
<li>
@Html.ActionLink(id.ToString(), "Index", new EmpID = id.ToString() )
</li>
</ul>
Finally, we get below output.
GitHub Project Link:- https://github.com/Sagar-Jaybhay/MVC5
Comments
Post a Comment