Wednesday 20 May 2015

ActionLink Method in Asp.net MVC

Here we will discuss about action Link Method in Asp.net MVC. Action Link method is a HTML helper which is used to provide the hyperlink. So Here we will generate hyperlink using ActionLink HTML helper for navigation b/w MVC pages.
Here we will show all the student name as Bullet List with the hyper link  when we click on that hyper link it should show respective student details.
Before coming in this tutorial please read previous tutorial.

Step(1).:-  In previous tutorial we have a Controller class name is “StudentController.cs” ther e we have a StudentDetail method for showing Student detail.  
Now we need a another method for showing Student name with the hyperlink, So for that add a another method to this class.
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class StudentController : Controller
    {

        public ActionResult Index()
        {
            StudentContext _context = new StudentContext();
            List<Student> _students = _context._studentDb.ToList();

            return View(_students);
        }
       
        public ActionResult StudentDetail(int id)
        {
            StudentContext _context = new StudentContext();
           Student _student =  _context._studentDb.Single(c => c.StudentId == id);

            return View(_student);
        }

    }
}



Step(2):-  We have View for StudentDetail method but we don’t have method for Index method so for index method create a View , For creating a view for index method right click on that method and add a method. Here you select Strongly typed view as Student from dropdownbox.



Step(3):-  After click on Add it will  add a view with the name as “Index.cshtml”. So you write following code in View



@model IEnumerable<MvcApplication1.Models.Student>

@using MvcApplication1.Models;
@{
    ViewBag.Title = "Student Names";
}
<div style="font-family: Arial">
    <h2>Student Names</h2>
    <ul>
    @foreach (Student _student in @Model)
    {
    <li>
    @Html.ActionLink(_student.StudentName,"StudentDetail",new {id = _student.StudentId})
   
    </li>
    }
    </ul>
</div>



Step(4) : Go to another StudentDetail View and add a link there also for coming back in Index view.



@model MvcApplication1.Models.Student

@{
    ViewBag.Title = "StudentDetail";
}

<h2>StudentDetail</h2>

<table style="font-family:Arial">
    <tr>
        <td>
            Student ID:
        </td>
        <td>
            @Model.StudentId
        </td>
    </tr>
    <tr>
        <td>
            Name:
        </td>
        <td>
            @Model.StudentName
        </td>
    </tr>
    <tr>
        <td>
            Gender:
        </td>
        <td>
            @Model.Gender
        </td>
    </tr>
    <tr>
        <td>
            City:
        </td>
        <td>
            @Model.SchoolName
        </td>
    </tr>
</table>

@Html.ActionLink("Back To Student Name List", "Index")


Step(5):- Now Run your application and navigate to following URL  the O/p Will be
http://localhost/MvcApplication1/student


Step(6):-  When you will click  on Name it will go to “StudentDetail” View with Respective ID.



For Index View or Student List click on “Back To Student Name List”.

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...