Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp action route data

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>
like image 495
stibay Avatar asked Aug 23 '16 08:08

stibay


People also ask

What is ASP all route data?

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.

How does ASP route work?

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.

How do I use MapControllerRoute?

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.

What is ASP controller and ASP action?

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.


1 Answers

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.

like image 76
TGlatzer Avatar answered Sep 28 '22 12:09

TGlatzer