4

Okay, I'm pretty new to ASP.Net / MVC 2. Can anyone explain how to use the Html.ActionLink thing? I understand that the first parameter is the displayed text, but for the second one, what is the action name??

DarkLightA
  • 14,980
  • 18
  • 49
  • 57

1 Answers1

2

User action in the asp.net MVC framework is based around Controllers and Actions that enable you to create pages (or links) to specific sections.

For example you might want a page to edit a Product so you have a Product Controller with an Edit Action. You can then create a Html ActionLink that will direct the user to this page.

In summary the 'action' will be the ActionResult method you want to direct your user to.

<%: Html.ActionLink("Edit Product", "Edit", "Product") %>

public class ProductController : Controller
{
    public ActionResult Index() // Index is your action name
    {
    }

    public ActionResult Edit(int id) // Edit your product
    {
    }
}
David
  • 15,150
  • 15
  • 61
  • 83
  • and in this case you could have a view in Index() with this: <%: Html.ActionLink("Click me", "Edit"); %> in the asp:Content? What would then be an example of something you could have in the Edit action? – DarkLightA Dec 18 '10 at 17:51
  • For a link to the edit page you would need <%: Html.ActionLink("click me", "Edit", "Product", new { id = 1 } %>. The Edit action in the product controller requires a value for 'id' so when you create your actionlink you need to specify the new {id = 1} (in this example). – David Dec 18 '10 at 17:56
  • So then how would I make this Edit action link to, let's say, HomeController (main page). And why would I need a variable declaration for it? – DarkLightA Dec 18 '10 at 18:08
  • 1
    @DarkLightA you would of course want to edit a product. and the product must have a unique id. the extra variable is actually the id of product which you are going to edit. – Zain Shaikh Dec 18 '10 at 18:20