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>
The asp-all-route-data attribute supports the creation of a dictionary of key-value pairs. The key is the parameter name, and the value is the parameter value. In the following example, a dictionary is initialized and passed to a Razor view. Alternatively, the data could be passed in with your model.
Routing is the process through which the application matches an incoming URL path and executes the corresponding action methods. ASP.NET Core MVC uses a routing middleware to match the URLs of incoming requests and map them to specific action methods.
MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.
asp-controller - This attribute assigns the controller name which is used for generating the URL. asp-action - This attribute is used to specify the controller action method name. If no value assigns against this attribute name, then default asp-action value in the controller will execute to render the view.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With