2

I keep getting a Compilation Error and can't find matching overloaded method. I've tried a couple ways (variable, variable.toString). Below is the latest try.

When I click on the day (ex: 2) on the calendar the ActionLink should send the querystring: "Index?day=2".

@{ string dayAsString = startCount.ToString();}
<div><span>@Html.ActionLink(@startCount.ToString, "Index?day=" + dayAsString , "Event")</span></div>
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95

2 Answers2

2

Do this

<div>
    <span>
        @Html.ActionLink(startCount.ToString(), "Index", new { day = startCount })
    </span>
</div>

The last parameter creates an anonymous object with the property day and value startCount. ActionLink knows to convert that into a querystring using the property name and the property value.

More details here http://msdn.microsoft.com/en-us/library/dd492936.aspx

Edit:

If you want to target a specific controller, do this

@Html.ActionLink(startCount.ToString(), "Index", new { controller = "Event", day = startCount })

You can also do this

@Html.ActionLink(startCount.ToString(), "Index", "Event", new { day = startCount }, null)

but I don't like passing null as a parameter.

Here's a list of all the overloads: http://msdn.microsoft.com/en-us/library/dd505040.aspx

You can also just cycle in the intellisense.

Omar
  • 39,496
  • 45
  • 145
  • 213
  • 1
    I just tested this and I'm getting the correct value. Can you make sure you copied my answer correctly? Generally speaking, when you see `length=X`, it means you're using the incorrect overload of `ActionLink`. See http://stackoverflow.com/questions/824279/why-does-html-actionlink-render-length-4 – Omar Apr 19 '12 at 15:59
  • Well, I need the ActionLink to go to the Event controller. So I added that to your first answer. – dotnetN00b Apr 19 '12 at 16:00
  • After reading the link, this is what I have: @Html.ActionLink(startCount.ToString(), "Index", new { controller = "Event" }, new { day = startCount }) – dotnetN00b Apr 19 '12 at 16:03
  • Absolutely correct. Note: those overloads don't give you the slightest clue that you can do what you've shown. Then again, I'm new to MVC. – dotnetN00b Apr 19 '12 at 16:14
0

This should work

@Html.ActionLink(@startCount.ToString,"Index","Yourcontroller",new { day=@startCount.ToString()} , null)

replace Yourcontroller with your controller name

Shyju
  • 214,206
  • 104
  • 411
  • 497