asp.net-mvc-3 - filterContext.Controller.ViewData.Model 在 ActionFilter.OnActionExecuted 中为 null

标签 asp.net-mvc-3 viewmodel action-filter t4mvc

我有一个 ASP.NET MVC 3 项目,其中包含模型绑定(bind)到 View 模型的 POST 操作

[HttpPost]
public virtual ActionResult Reply(ReplyViewModel viewModel)
{
    // ...
}

我有一个 ActionFilter,我想在 OnActionExecuted 中查看该 View 模型,但 ViewData.Model 为空。我是否误解了 ViewData.Model 是什么?

public class CopyViewModelToTempDataAttribute : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
    {
        // filterContext.Controller.ViewData.Model is null
    }
}

最佳答案

这可能是这个问题的解决方案 1. 我假设您分配了 Model in Action

现在来过滤

public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
    {
        if(filterContext.HttpContext.Request.HttpMethod == "POST")
        {
        // Get your model here.
        }
    }

关于asp.net-mvc-3 - filterContext.Controller.ViewData.Model 在 ActionFilter.OnActionExecuted 中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549814/

相关文章:

asp.net-mvc - 为什么要使用 View 模型?

asp.net-mvc - MVC 3 中如何处理 session 超时

asp.net-mvc - 获取 ActionName、ControllerName 和 AreaName 并传入 ActionFilter 属性

validation - MVC 3为远程验证指定验证触发器

javascript - 如何获取自定义 mvc3 属性以验证客户端

javascript - 单击链接可放大图像

java - Fragment 无法从 ViewModel 获取值

android - _uiState.value = uiState.value.copy() 不会导致重组

asp.net - Application_MapRequestHandler 有时真的很慢

c# - 如何在操作过滤器中记录所有查询参数?