c# - 如何使用@Html.ActionLink 显示带参数的索引视图?
问题描述
当我使用@ActionLink
Index ActionName 时,此 ActionName 不会出现在浏览器链接中。
@Html.ActionLink(linkText: "return", actionName: "Index", controllerName: "NewsImages"
, routeValues: new { selectedNewsid = 1 }, htmlAttributes: null)
此操作在浏览器上显示以下链接: “ http://localhost:23594/NewsImages/?selectedNewsid=1 ” 但是当我使用另一个 ActionName 时,链接正确显示!我究竟做错了什么?这是我的路由配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "GPTKish.Controllers" }
);
}
解决方案
如果您在 ActionLink 中提供的操作名称或 id 类似于在 routes.MapRoute 中维护的默认名称,那么它在 URL 中不可见,并且有时会产生问题。如果您想使用索引作为操作名称,那么只需在索引之前添加 /
@Html.ActionLink(linkText: "return", actionName: "/Index", controllerName: "NewsImages"
, routeValues: new { SelectedNewsid = 1 }, htmlAttributes: null)
(PS:在演示中,我将控制器名称更改为 Home)
推荐阅读
- c++ - 在 c++ 中重载小于运算符以在 std::sort 中使用
- r - 将最后一列移动到 R 中的第 n 个位置
- python - 使用正则表达式获取指定字符串之间的所有文本
- css - 如何使用 react-native 和 align 让 flex 元素缩小到它们的内容?
- eclipse - 在 Tomcat 上的 Eclipse 中启动 Springboot 应用程序
- javascript - 如何在 React 中处理多个 DIV 的点击事件?
- java - 我如何在 JDK 8 中运行这个简单的代码?
- c++ - 长转换 C++ arduino
- java - 如何在 kotlin 中查找通用类名
- excel - 尝试使用 API 函数将从文件中提取的图标设置为 TreeView Node.Image