我正在用C#编写MVC Web应用程序。

如何从视图调用控制器上的操作,而该操作不在加载视图的控制器中?

@Html.ActionLink("Edit", "Edit", new { id=item.BookID }) |
@Html.ActionLink("Details", "Details", new { id=item.BookID }) |
@Html.ActionLink("Add Comment","Create", "CommentController", new { bookid=item.BookID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.BookID })


上面的代码是从Book控制器加载的。我想在CommentController中调用“创建”操作(请参见上述代码的第3行)。

当我单击上面的代码时,以下页面链接到:
serveraddress / Book / Create?Length = 17

我正在尝试链接到:
serveraddress /注释/创建?长度= 17

最佳答案

问题是您使用了错误的构造函数。更换...

@Html.ActionLink("Add Comment","Create", "CommentController", new { bookid=item.BookID })


与...

@Html.ActionLink("Add Comment","Create", "CommentController", new { bookid=item.BookID }, null)


我只在构造函数的参数列表末尾添加了null

关于c# - 链接到 View 中的其他 Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20672232/

10-10 04:37