22

In the old version of MVC 5 I could do this to pass route parameters

@Html.ActionLink("New item", "Suggestion", new ProductSuggestion() { Username = Model.Username }, new { @class = "btn btn-default" })

I am trying to get this to work with the new asp-action method and I figgured out I could do this as a workaround. But what is the correct syntax for this?

    @{ 
        var a = new Dictionary<string, string> { { "Username", Model.Username } };
    }

    <a asp-action="Suggestion" asp-all-route-data="a" class="btn btn-default">New item</a>
Tseng
  • 61,549
  • 15
  • 193
  • 205
stibay
  • 1,200
  • 6
  • 23
  • 44
  • Maybe [this](http://stackoverflow.com/questions/30020892/taghelper-for-passing-route-values-as-part-of-a-link) could help you. – diiN__________ Aug 23 '16 at 08:59
  • There is no MVC6 (anymore)!! https://blogs.msdn.microsoft.com/webdev/2016/01/19/asp-net-5-is-dead-introducing-asp-net-core-1-0-and-net-core-1-0/ – Tseng Aug 23 '16 at 10:00
  • That actually doesn't say anything about what MVC will be called. .Net Core 1.0 is just the framework. – stibay Aug 23 '16 at 10:47
  • Possible duplicate of [TagHelper for passing route values as part of a link](https://stackoverflow.com/questions/30020892/taghelper-for-passing-route-values-as-part-of-a-link) – The Bearded Llama Nov 23 '17 at 10:56

2 Answers2

51

Dave Paquette did a nice write up about many of the TagHelpers. Here's the one for the anchor tag helper: http://www.davepaquette.com/archive/2015/06/01/mvc-6-anchor-tag-helper.aspx

Essentially you can do

<a asp-controller="MyController" asp-action="MyAction" asp-route-myvar="myValue">

Which will then provide myValue to a controller method like

public Task<IActionResult> MyAction(string myVar) { 
...
} 

Be aware that you cannot do this with complex types that easily.

Tony Abrams
  • 4,505
  • 3
  • 25
  • 32
TGlatzer
  • 5,815
  • 2
  • 25
  • 46
0

I believe you still can. I think what's missing is the inclusion of the controller in your anchor above.

@{ 
    var a = new Dictionary<string, string> { { "Username", Model.Username } };
}

<a asp-controller="{Controller}" asp-action="Suggestion" asp-all-route-data="a" class="btn btn-default">New item</a>
BDC
  • 63
  • 1
  • 9