我可以从ActionResult中检索ViewModel吗?

Sam*_*amo 3 c# asp.net-mvc actionresult viewmodel

试图避免在这里重复.我在基类控制器中有一个动作,我不允许修改.我希望我的动作做一些检查,调用基类操作,并在渲染之前以某种方式修改结果.但我需要做的部分工作包括修改一些属性,ViewModel基类返回一个ActionResult.我看不出有什么办法让ViewModelActionResult,所以我可能会写一个自定义的方法,其中大部分只会模仿什么的基类是做.我强烈不愿意这样做.有什么建议?

sup*_*ass 9

那是因为它ActionResult是一个相当高级的基类.尝试将其转换为适当的子类型,例如ViewResult.

快速示例代码:

    public ActionResult WrapperAction()
    {
        // do your initial stuff


        // call your base controller action and cast the result
        // it would be safer to test for various result types and handle accordingly
        ViewResult result = (ViewResult)base.SomeAction();

        object model = result.ViewData.Model;

        // do something with the model

        return result;
    }
Run Code Online (Sandbox Code Playgroud)