如何解决如何在C#中将参数传递给URL?
我想知道如何将参数附加到请求URL。假设我有一个带有一些链接的表格:
当我单击Steve的“编辑”时,我得到了以下URL:
URL末尾附加有'3'作为StudentID。它是怎么发生的?
我检查了我的RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Student",url: "student/{id}",defaults: new { controller = "Student",action = "Index"}
);
routes.MapRoute(
name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional }
);
}
“学生/”旁边的{id}可以替换为任何东西,这里的名称无关紧要。
我查看了我的视图
@model IEnumerable<TestAPP.Models.Student>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New","Create")
</p>
<table class="table">
<tr>
<th>
@Html.displayNameFor(model => model.StudentName)
</th>
<th>
@Html.displayNameFor(model => model.Age)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.displayFor(modelItem => item.StudentName)
</td>
<td>
@Html.displayFor(modelItem => item.Age)
</td>
<td>
@Html.ActionLink("Edit","Edit",new { id = item.StudentId }) |
@Html.ActionLink("Details","Details",new { id = item.StudentId }) |
@Html.ActionLink("Delete","Delete",new { id = item.StudentId })
</td>
</tr>
}
</table>
即使我可以看到分配给“编辑”链接的“ StudentId”项(显示在我的代码底部),也无法确定单击“ ID”参数后是否将“ Id”参数附加到URL编辑”
解决方法
@Html.ActionLink("Edit","Edit",new { id = item.StudentId })
创建与每个“项目”的编辑页面关联的URL。 @Html.ActionLink
是对razor页面库的指令,该指令执行将参数与url字符串连接起来的函数。 new { id = item.StudentId }
根据documentation提供到函数的路由值。您的路由器需要一个可选的{id}
参数来路由到特定资源,并且它将在uri末尾查找该值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。