使用C#mvc添加链接

时间:2013-02-21 19:47:57

标签: c# asp.net-mvc html-helper

我有一个可访问的视图http://localhost:49467/About/,其名为index.cshtml

我有另一个视图,http://localhost:49467/Food/可以访问该视图,并被称为index2.cshtml

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

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

但我最终在网址http://localhost:49467/About/Food/中找不到该网页。我想要做的就是导航到http://localhost:49467/Food/。我怎么能纠正这个?

2 个答案:

答案 0 :(得分:3)

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

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

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

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

上面的第三个参数是关联操作的控制器名称。

如果index.cshtml也与食物控制器相关联,则无需指定控制器名称:

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

答案 1 :(得分:1)

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

注意额外的斜线。 或者看起来你的路线决定/ Food使用index2.cshtml。所以你应该能够逃脱

@Html.ActionLink("link", "index2", "Food" ) 

使用MVC,您不必担心视图的文件名,而是需要通过哪些控制器和操作来访问该视图。