C# 来自Html.ActionLink的控制器参数
在主视图上,我生成了许多链接:C# 来自Html.ActionLink的控制器参数,c#,html,asp.net,asp.net-mvc-5,C#,Html,Asp.net,Asp.net Mvc 5,在主视图上,我生成了许多链接: <ul class="nav navbar-nav"> @foreach (var item in ViewBag.RegList) { <li>@Html.ActionLink((string)item.registryName, "Index", "Registry", new { item.registryName }, new { ite
<ul class="nav navbar-nav">
@foreach (var item in ViewBag.RegList)
{
<li>@Html.ActionLink((string)item.registryName, "Index", "Registry", new { item.registryName }, new { item.registryID }) </li>
}
</ul>
@foreach(ViewBag.RegList中的变量项)
{
- @ActionLink((字符串)item.registryName,“索引”,“注册表”,新的{item.registryName},新的{item.registryID})
}
我不明白-如何在控制器的ActionLink中设置参数,以及它们从何处开始
这就是我在me控制器中定义索引的方式:
public async Task<ActionResult> Index(object attr)
公共异步任务
或者这个:如果查看创建的链接,它们将包含一组查询字符串参数
"https://some.where/controller/SomeAction/7788?extraParam1=foo&extraParam2=bar"
MVC的标准路由配置使参数“id”成为本地路径的一部分(本例中为“7788”),而其他参数则添加到查询字符串中(问号之后)
操作的方法签名应该是
public async Task<ActionResult>
SomeAction(string id, string extraParam1, string extraParam2)
公共异步任务
SomeAction(字符串id、字符串extraParam1、字符串extraParam2)
获取我的示例链接中的参数。OP最终可以将其操作的attr
重命名为id
,或者将默认参数名称更改为attr
,您是对的,但问题是如何访问值,因此我认为需要更多的解释。哦,因此,控制器中参数的名称很重要。非常感谢您为第4个参数添加了new{attr=item.registryName}
,方法将是public async Task Index(string attr)
(第5个参数用于添加html属性,也需要遵循相同的格式,如何将这些html属性从第5个参数获取到我的控制器?您不知道-它用于添加html属性,例如,将类名添加到链接,它将是new{@class=“form control”}
。我假设您要向方法传递多个参数,在这种情况下,第四个参数将是new{someParameterName+Item.SomeProperty,anotherParameterName=Item.AnotherProperty}