0

I am trying to output the following URL using the ASP.Net MVC3 framework and I am a total noob at this stuff:

http://www.foo.com/controller/action/1

Here is my code so far:

@Html.ActionLink("Click here!", "action", new { Controller = "controller" })

This gives me:

http://www.foo.com/controller/action/

What do I need to get a simple hardcoded 1 in there at the end?

Allen Liu
  • 3,948
  • 8
  • 35
  • 47

2 Answers2

3

To specify route parameters you'll need to use the following signature

@Html.ActionLink("Click here!", "action", "controller", new {id = 1}, null })

The null at the end represents the HtmlAttributes which you can specify if you like

Also checkout this question "HTML.ActionLink method" for a great description of how ActionLink should be used

Community
  • 1
  • 1
brodie
  • 5,354
  • 4
  • 33
  • 28
  • I tried this and I get a different controller name instead plus I get a weird `Length=5`. – Allen Liu Sep 16 '11 at 02:09
  • 1
    it would depend on how you've set up your routes and your action method. Read the linked question which will give you some more tips ... or show the routing and action method code :) – brodie Sep 16 '11 at 02:13
  • oops...forgot the `null` at the end. – Allen Liu Sep 16 '11 at 02:13
1

You need to add another routing parameter:

@Html.ActionLink("Click here!", "action", new { Controller = "controller", id = 1 })

I'm assuming that you're trying to use a route of the form "{controller}/{action}/{id}"

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    I don't think its standard to specify the controller in the route parameters – brodie Sep 16 '11 at 02:00
  • Sweet! Thanks. I was using `@bar=1` which gave me `http://www.foo.com/controller/action?bar=1`. – Allen Liu Sep 16 '11 at 02:02
  • I am doing this in a partial view which is basically my nav bar. There is no specific controller or model that this is tied to. What would be the standard way of going about this? – Allen Liu Sep 16 '11 at 02:03
  • 1
    @Allen: Call the overload that takes the controller name as a string. You'll also need to pass `null` for the `htmlAttributes` parameter. – SLaks Sep 16 '11 at 02:08
  • @SLaks Thanks! Works nicely now. – Allen Liu Sep 16 '11 at 02:12