MVC Model在OnExecuted操作筛选器中为null ...还是更优雅的方式来设置模型? [英] MVC Model is null in OnExecuted action filter ... or a more elegant way to set the model?

查看:87
本文介绍了MVC Model在OnExecuted操作筛选器中为null ...还是更优雅的方式来设置模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ActionFilter,它在OnActionExecuted方法上有一个覆盖. filterContext.Controller.ViewData.Model在POST操作上始终为null.我确实找到了下面的文章,似乎是在说它不应该为null,但是它必须是MVC的早期版本.这是MVC3.我应该得到什么?

I have an ActionFilter with an override on the OnActionExecuted method. The filterContext.Controller.ViewData.Model is always null on a POST operation. I did find the following article that seems to be saying that it should not be null but this must have been an earlier version of MVC. This is MVC3. What should I be getting?

ActionFilter内部的模型可用性

更新:

我已经找到了原始问题的答案.我有一个自定义ActionResult,可使用自定义日期格式器输出JSON.问题是没有在控制器中设置模型.

I've figured out the answer to the original question. I had a custom ActionResult that outputs JSON with a custom date formatter. The problem was that the model is not being set in the controller.

在我自定义的ActionResult中,ExecuteResult方法通过了ControllerContext,如果可以在此处设置Model,那就很好了.

In my custom ActionResult the ExecuteResult method get passed the ControllerContext which would be nice if I could set the Model there:

context.Controller.ViewData.Model = _data;

但是这已经到了周期的最后,并且在ActionFilter中结果仍然为null.这似乎意味着我需要在控制器中手动设置模型:

But this is to late in the cycle and the result is still null in the ActionFilter. This seems to mean that I need to manually set the model in the controller:

ControllerContext.Controller.ViewData.Model = model; 

View(model);

那么,这意味着我每次使用此自定义ActionResult时都要记住要执行此操作.有没有更优雅的方式?

Which then means I need to remember to do this every time I use this custom ActionResult. Is there a more elegant way?

还有其他更新:

我找到了一种做到这一点的方法,但它并不像我希望的那样优雅.

I found a way to do this it just isn't as elegant as I hoped.

在我发送给控制器的可操作ActionResult的构造函数中,至少这样,它将始终保持一致:

In my constructor for the comstom ActionResult I sending in the controller, that way at least it will alway be consistent:

public JsonNetResult(object data, Controller controller) {
    SerializerSettings = new JsonSerializerSettings();
    _data = data;
    controller.ControllerContext.Controller.ViewData.Model = _data;
}

推荐答案

另一种方法是使用基本控制器自动处理动作参数集合的存储以供以后使用:

Another approach is to use a base controller to automatically handle the storing of the action parameters collection for later use:

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Items["ActionParms"] = filterContext.ActionParameters.ToDictionary(p => p.Key, p => p.Value);
        base.OnActionExecuting(filterContext);
    }
}

然后在您的属性中

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    var dictionary = filterContext.HttpContext.Items["ActionParms"] as Dictionary<string, object>;
    if (dictionary != null)
    {
        foreach (var o in dictionary.Keys)
        {
            // do something here
        }   
    }            
    base.OnActionExecuted(filterContext);
}

它使用的HttpContext项目不是很好,但是我不知道您可以在属性中访问ViewBag或ViewData.

It uses HttpContext items which is not very nice but I don't know that you can access your ViewBag or ViewData in the attribute.

为了确定是否要在属性中处理请求,可以询问操作名称和其他参数信息:

In order to decide whether you want to handle the request in your attribute, you can interrogate the action name and other parameter information:

var action = filterContext.ActionDescriptor.ActionName;
var parms = filterContext.ActionDescriptor.GetParameters();
foreach (var parameterDescriptor in parms)
{
    // do something here
}

这篇关于MVC Model在OnExecuted操作筛选器中为null ...还是更优雅的方式来设置模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆