c# - 创建一个基本 View 模型,但似乎无法在 OnActionExecuting 中 Hook 它

标签 c# asp.net-mvc asp.net-mvc-4 viewdata

我创建了一个 baseviewmodel,我的其他强类型 View 模型继承自该模型。

基础 Controller :

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
            var baseViewModel = ViewData.Model as BaseViewModel;
            if (baseViewModel != null)
            {
                // set common properties that I want to use in all views
            }
}

现在,当我在 if 子句上设置断点时,baseViewModel 似乎始终为 null。

如何将基础 ViewData.Model 设置为 BaseViewModel?

最佳答案

OnActionExecuting现在看模型还为时过早。

Called before the action method is invoked.

您可以在 OnActionExecuted 中看到模型。

关于c# - 创建一个基本 View 模型,但似乎无法在 OnActionExecuting 中 Hook 它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16179391/

相关文章:

asp.net - web.config 编译/程序集元素有什么作用?

c# - 在组合框中获取数据库表

并排使用 telerik 扩展和 kendo ui 时发生 Jquery 冲突

c# - 为什么 `List<T>` 不可克隆?

c# - 将 xUnit.NET ASP.NET MVC 单元测试添加到现有项目/解决方案

asp.net-mvc - 命名约定 - Controller 的一项规则,模型和 View 没有规则

asp.net-mvc - 如何在浏览器关闭 MVC ASP .NET 上结束 session 或注销用户

c# - ASP.NET MVC 4 模型绑定(bind)问题

c# - TSQL BULK INSERT 行错误导致 C# 异常

c# - 使用 HTML Agility Pack 将单个节点替换为多个节点