174

I have a lot of experience with ASP.NET MVC 1-5. Now I learn ASP.NET Core MVC and have to pass a parameter to link in page. For example I have the following Action

 [HttpGet]
 public ActionResult GetProduct(string id)
 {
      ViewBag.CaseId = id;
      return View();
 }

How can I implement the link for this action using tag helpers?

<a asp-controller="Product" asp-action="GetProduct">ProductName</a>

4 Answers 4

317

You can use the attribute prefix asp-route- to prefix your route variable names.

Example:

<a asp-controller="Product" asp-action="GetProduct" asp-route-id="10"> ProductName</a>
7
  • 2
    If I have multiple parameter, I must add everything with this method? Commented Jun 27, 2016 at 6:22
  • 53
    @elvin-mammadov, yup, using asp-route-yourParamName, for example: asp-route-foo="bar"
    – Alex
    Commented Jun 27, 2016 at 6:24
  • 1
    @Alex Your example will generate the html as: <a href="/Product/GetProduct/10">ProductName</a>. Question: While using anchor tag helper how can we get the parameter value (e.g. 10) using jquery? `
    – nam
    Commented Feb 2, 2017 at 21:51
  • 4
    @nam You can add a data attribute next to the ASP helper. <a asp-route-id="@item.Id" data-id="@item.Id" /> and then get the id with $('a').attr('data-id') :) Commented May 15, 2017 at 19:34
  • 2
    what if i want to add 5 Query parameters programmatically?? Commented Nov 18, 2017 at 13:47
42

You might want to apply the following syntax.

<a asp-controller="Member"
   asp-action="Edit"
   asp-route-level="3"
   asp-route-type="full"
   asp-route-id="12">Click me</a>

That will produce the call route like this.

/Member/Edit/3/full/12

Then you can receive it in the method as shown below.

[Route({level}/{type}/{id})]
public IActionResult Edit(int level, string type, int id) { ... }

Although, the attribute decorating the method isn't required in MVC, it shows more clearly how to bind the attributes from the link to the passed in parameters in the method.

2
  • 1
    For me instead of /Member/Edit/3/full/12 it generates /Member/Edit/3?type=full&id=12
    – Arif
    Commented Nov 22, 2019 at 4:02
  • 1
    @Arif Can't say for sure why and I'm not in a C# environment at the moment (too lazy to start up one too). However, I'd suggest that it depends on whether we're explicitly stating [FromUrl] or [FromQuery] etc., which might differ between different versions of the .NET palatform and/or depend on inheriting classes for pure WebAPI calls (as opposed to the classes for BaseController with views and all that). Just a guestimation, NB. Commented Nov 22, 2019 at 16:22
9

if you want to put variable id into link in grid or table something below code can be used

[HttpGet]
[Route("/Product/GetProduct/{id}")]
 public ActionResult GetProduct(string id)
 {
      ViewBag.CaseId = id;
      return View();
 }


 <a  asp-controller="Product" asp-action="GetProduct" asp-route-id="@item.id" >ProductName</a>
0
0

On the back-end:

This code must write at the top of the action in the controller

[Route("/Controller/Method/{Object or varible name}")]
public actionresult method name(your variable)
{
    //your code...
}

On the front-end:

@{
var url = "/Controller/Method/" + your data;
<a href="@url"> click me for send data()</a>
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.