How to add link parameter to asp tag helpers in ASP.NET Core MVC

How to add link parameter to asp tag helpers in ASP.NET Core MVC

问题

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>

 

回答

ou 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>

 

@elvin-mammadov, yup, using asp-route-yourParamName, for example: asp-route-foo="bar"
– Alex
Jun 27, 2016 at 6:24
 
 
 
回答2

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.

 

 

 
 
 
 
 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-08-03 在HearthRanger中使用Silverfish
2019-08-03 ReSharper “Cannot resolve symbol” even when project builds
2018-08-03 problem in Sourcetree
2018-08-03 Get started with Sourcetree
2015-08-03 Fixing common issues when hosting a .NET 4.0 WCF service in IIS 7
2015-08-03 一句话的设计模式
2015-08-03 wcf纯代码创建控制台应用
点击右上角即可分享
微信分享提示