我可以从 ActionResult 检索 ViewModel 吗?
在这里尽量避免重复。我在基类控制器中有一个不允许修改的操作。我希望我的操作执行一些检查,调用基类操作,并在渲染之前以某种方式修改结果。但我需要做的部分工作涉及修改 ViewModel
的一些属性,并且基类返回一个 ActionResult
。我看不出有什么办法可以从 ActionResult
获取 ViewModel
,因此我可能必须编写一个自定义方法,其中大多数方法只是模仿基类正在执行的操作。我强烈不想这样做。有什么建议吗?
Trying to avoid repetition here. I have an action in a base class controller which I am not allowed to modify. I'd like my action to do some checks, call the base class action, and modify the result in some way before rendering. But part of what I need to do involves modifying some properties of the ViewModel
, and the base class returns an ActionResult
. I see no way to get the ViewModel
from the ActionResult
, and so I may have to write a custom method, most of which would just mimic what the base class is doing. I'd strongly prefer not to do this. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
ActionResult
是一个相当高级的基类。尝试将其转换为适当的子类型,例如ViewResult
。快速示例代码:
That's because
ActionResult
is a fairly high-level base class. Try casting it to the appropriate subtype, such asViewResult
.Quick sample code: