C# ASP.NET MVC:从视图访问控制器实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1417168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC: Access controller instance from view
提问by Alex
How can I access a controller instance from the view? E.g. I have a HomeController
which then returns my Index
view. Inside of that view I want to access the HomeController
instance that created the view. How do I do that?
如何从视图访问控制器实例?例如,我有一个HomeController
然后返回我的Index
观点。在该视图中,我想访问HomeController
创建该视图的实例。我怎么做?
采纳答案by David Andres
ViewContext.Controller, and you'll need to cast it.
ViewContext.Controller,你需要转换它。
<% var homeController = ViewContext.Controller as HomeController; %>
This is covered with a few extra wrinkles in post Asp.Net MVC: How do I get virtual url for the current controller/view?.
这在Asp.Net MVC帖子中包含了一些额外的皱纹:如何获取当前控制器/视图的虚拟 url?.
EDIT: This is to add some meat to Mark Seemann's recommendation that you keep functionality out of the view as much as humanly possible. If you are using the controller to help determine the markup of the rendered page, you may want to use the Html.RenderAction(actionName, controllerName)
method instead. This call will fire the action as though it was a separate request and include its view as part of the main page.
编辑:这是在 Mark Seemann 的建议中添加一些内容,即您尽可能多地将功能排除在视线之外。如果您使用控制器来帮助确定呈现页面的标记,您可能需要使用该Html.RenderAction(actionName, controllerName)
方法。此调用将触发该操作,就好像它是一个单独的请求一样,并将其视图包含为主页的一部分。
This approach will help to enforce separation-of-concerns because the action method redirected to can do all the heavy lifting with respect to presentation rules. It will need to return a Partial View to work correctly within your parent view.
这种方法将有助于实施关注点分离,因为重定向到的操作方法可以完成与表示规则相关的所有繁重工作。它需要返回一个局部视图才能在您的父视图中正常工作。
回答by Mark Seemann
In my opinion, you should consider a design where the View doesn't need to know about the Controller. The idea is that the Controller deals with the request, conjures up a Model and hands that Model off to the View. At that point, the Controller's work is done.
在我看来,您应该考虑这样一种设计,其中 View 不需要了解 Controller。这个想法是控制器处理请求,召唤一个模型并将该模型交给视图。至此,Controller 的工作就完成了。
I think it is an indication of a design flaw if the View needs to know anything about the Controller. Can you share more about what it is that you are trying to accomplish?
如果 View 需要了解有关 Controller 的任何信息,我认为这表明存在设计缺陷。你能分享更多关于你正在努力实现的目标吗?
I often find that when dealing with well-designed frameworks (such as the MVC framework), if it feels like the framework is fighting you, you are probably going about the task in the wrong way. This has happened to me a lot, and stepping back and asking myself what it is that I'm really trying to accomplish often leads to new insights.
我经常发现,在处理设计良好的框架(例如 MVC 框架)时,如果感觉框架在与您作对,那么您很可能以错误的方式执行任务。这在我身上发生过很多次,退后一步问问自己,我真正想要完成的是什么,这往往会带来新的见解。