How do I append a variable to a string in an ActionLink?

后端 未结 2 539
孤城傲影
孤城傲影 2021-01-24 03:39

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

相关标签:
2条回答
  • 2021-01-24 03:52

    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.

    0 讨论(0)
  • 2021-01-24 04:08

    This should work

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

    replace Yourcontroller with your controller name

    0 讨论(0)
提交回复
热议问题