首页 > 编程语言 >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

时间:2023-08-03 16:14:42浏览次数:33  
标签:Core asp route MVC link ASP NET

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.

 

 

         

 

标签:Core,asp,route,MVC,link,ASP,NET
From: https://www.cnblogs.com/chucklu/p/17603597.html

相关文章

  • Spark Core源码分析: RDD基础
    RDD RDD初始参数:上下文和一组依赖1.abstractclass2.@[email protected] 以下需要仔细理清:AlistofPartitionsFunctiontocomputesplit(subRDDimpl)AlistofDependenciesPartitionerforK-VRDDs(Optional)Preferredl......
  • asp.net core之异常处理
    在开发过程中,处理错误是一个重要的方面。ASP.NETCore提供了多种方式来处理错误,以确保应用程序的稳定性和可靠性。TryCatchTryCatch是最常见也是最基础的一种异常处理方式,只需要用TryCatch把执行代码包起来,即可捕获异常。格式如下:try{//执行操作doAny();}catch......
  • AddMvcCore,AddControllers,AddControllersWithViews,AddRazorPages的区别
    AddMvc/AddMvcCore/AddControllers等区别1.services.AddMvcCore()只注册运行 Controller/RazorPages 必要的核心服务,确保 Pipeline 程序可动作,其馀如像 DataAnnotationModelValidation、身分验证等服务要自己加挂,除有特殊客制需求,一般不太常用。2.services.AddControl......
  • [回馈]ASP.NET Core MVC开发实战之商城系统(五)
    经过一段时间的准备,新的一期【ASP.NETCoreMVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习......
  • 【HMS Core】【Push Kit】每天只能收到两条推送、状态码80100018
    【问题描述1】每天只能收到2条推送消息,其余的都无法收到【解决方案】1、请是否开通了消息自分类,因为现在是有咨询营销类消息限制的。没有使用自分类权益的话默认是资讯营销类消息。https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/message-restriction-d......
  • 【HMS Core】位置服务逆地理编码请求错误问题
    【关键字】HMS、位置服务、逆地理编码【问题描述】有开发者反馈在集成位置服务-逆地理编码时,出现了请求报错的问题。后端请求逆地理编码错误{   "returnCode":"010010",   "returnDesc":"INVALID_REQUEST"}【问题分析】1、一开始认为是cp的请求参数有误,缺少了必选的参数。......
  • SpringMVC入门案例
    坐标<!--Spring坐标--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version><......
  • asp.net中获得客户端的IP地址
    原文链接:https://blog.csdn.net/as2712101/article/details/9357777今天看到的一个方法,来查下: 通常我们都通过下面的代码获得IP:   string ip=System.Web.HttpContext.Current.Request.UserHostAddress;    或 string ip=System.Web.HttpContext.Current.Request.S......
  • .NET Core WebAPI中使用Swagger(完整教程)
    一、Swagger简介1.1-什么是Swagger?Swagger是一个规范且完整的框架,用于生成、描述、调试和可视化Restfull风格的Web服务。Swagger的目标是对RestAPI定义一个标准且和语言无关的接口,可以让人和计算机拥有无需访问源码、文档或网络流量监控就可以发现和连接服务的能力。当通过......
  • .NET Core WebAPI中使用Swagger(完整教程)
    一、Swagger简介1.1-什么是Swagger?Swagger是一个规范且完整的框架,用于生成、描述、调试和可视化Restfull风格的Web服务。Swagger的目标是对RestAPI定义一个标准且和语言无关的接口,可以让人和计算机拥有无需访问源码、文档或网络流量监控就可以发现和连接服务的能力。当通过S......