1

I'm working on Visual Studio MVC currently. As MVC helps to create codes automatically for some functions. I am trying to understand what does this sentence of code means. I understand the first two parameters, but not the third one.

-----> @Html.ActionLink("Text Displayed", "Method name in controller", "Third???")

My code for my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MP.Controllers
   {
public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

Below is the code I do not understand:

<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
Vincent
  • 61
  • 1
  • 3
  • 8

2 Answers2

3

The 3rd parameter is the name of the controller. In your case you have a controller named HomeController so the 3rd parameter is "Home" (by convention, you omit the "Controller" suffix).

In the case where the page the @Html.ActionLink() method is used has been generated by a method in HomeController, you can omit the 3rd parameter, for example, in your About page, you can use

<li>@Html.ActionLink("Home", "Index")</li>

however if the Index() method was in (say) ProductController, then it would need to be

<li>@Html.ActionLink("Home", "Index", "Product")</li>
2

You could see there in your intellisense system -

enter image description here

It is clearly mentioned here- string controllerName.

Moreover, There are sever 10 types of extensions you can get with this helper.

Manoz
  • 6,507
  • 13
  • 68
  • 114