Skip to main content
 首页 » 编程设计

c#之使用 C# mvc 添加链接

2024年02月20日12think

我有一个可访问的 View http://localhost:49467/About/ 及其名为 index.cshtml 的 View 。

我有另一个 View ,可通过 http://localhost:49467/Food/ 访问,名为 index2.cshtml

我想在 index.cshtml 页面中放置一个链接,这样我就可以访问 index2.cshtml。我该怎么做。我的代码;

@Html.ActionLink("link", "Food/index" )  

但我最终进入了 URL http://localhost:49467/About/Food/ 并且找不到该页面。我只想导航到 http://localhost:49467/Food/。我该如何纠正这个问题?

请您参考如下方法:

'ActionLink 助手正在调用 Controller 操作,而不是静态 URL

@HTML.ActionLink("linktext", "ControllerName") 

您的 Index2.cshtml 是否与 Food Contoller 的 Index 操作相关联?如果是这样,您可以这样做:

@Html.ActionLink("Linktext", "Index2", "Food", null, null) 

上面的第三个参数是关联 Action 的 Controller 名称。

如果 index.cshtml 也与食品 Controller 关联,则无需指定 Controller 名称:

@Html.ActionLink("Linktext", "Index2")