[英]How to add link parameter to asp tag helpers in ASP.NET Core MVC
我在ASP.NET MVC 1-5方面有很多经验。 现在我学习了ASP.NET Core MVC并且必须传递一个参数来链接页面。 例如我有以下操作
[HttpGet]
public ActionResult GetProduct(string id)
{
ViewBag.CaseId = id;
return View();
}
如何使用标签助手实现此操作的链接?
<a asp-controller="Product" asp-action="GetProduct">ProductName</a>
您可以使用属性前缀asp-route-
为您的路由变量名称添加前缀。
示例:
<a asp-controller="Product" asp-action="GetProduct" asp-route-id="10"> ProductName</a>
您可能希望应用以下语法。
<a asp-controller="Member"
asp-action="Edit"
asp-route-level="3"
asp-route-type="full"
asp-route-id="12">Click me</a>
这将产生这样的呼叫路由。
/成员/编辑/3/full/12
然后就可以通过如下所示的方法接收了。
[Route({level}/{type}/{id})]
public IActionResult Edit(int level, string type, int id) { ... }
虽然在MVC中不需要装饰方法的属性,但它更清楚地展示了如何将来自链接的属性绑定到方法中传入的参数。
如果您想将变量 id 放入网格或表格中的链接中,可以使用以下代码
[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>
在后端:
这段代码必须写在控制器中动作的顶部
[Route("/Controller/Method/{Object or varible name}")]
public actionresult method name(your variable)
{
//your code...
}
在前端:
@{
var url = "/Controller/Method/" + your data;
<a href="@url"> click me for send data()</a>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.