C# ActionFilter OnActionExecuted和TempData

C# ActionFilter OnActionExecuted和TempData,c#,asp.net-mvc-4,action-filter,C#,Asp.net Mvc 4,Action Filter,我的目标是用Id记录一些控制器操作 然后我创建了一个日志过滤器,将其用作方法属性 public class LogFilter : ActionFilterAttribute, IActionFilter { public TypeLog Type { get; set; } public String Libelle { get; set; } public override void OnActionExecuted(ActionExecutedContext fi

我的目标是用Id记录一些控制器操作

然后我创建了一个日志过滤器,将其用作方法属性

public class LogFilter : ActionFilterAttribute, IActionFilter
{
    public TypeLog Type { get; set; }
    public String Libelle { get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        int idhospit = 0;

        object retour = filterContext.Result as JsonDotNetResult;
        if (retour == null)
            retour = filterContext.Result as JsonResult;
        if (retour == null)
            retour = filterContext.Result as PartialViewResult;

        object data = null;
        bool success = false;
        if (retour != null && retour is PartialViewResult)
        {
            PartialViewResult pvr = retour as PartialViewResult;
            data = new { Success = true, id = pvr.ViewData["id"] };
        }
        else if (retour != null)
        {
            data = retour.GetType().GetProperty("Data").GetValue(retour, null);
        }

        if (retour != null
            && data != null
            && data.GetType().GetProperty("Success") != null
            && data.GetType().GetProperty("Success").GetValue(data, null) != null)
        {
            success = (bool)(data.GetType().GetProperty("Success").GetValue(data, null));
        }

        if (success
            && retour != null
            && data != null
            && data.GetType().GetProperty("id") != null
            && data.GetType().GetProperty("id").GetValue(data, null) != null)
        {
            var id = (data.GetType().GetProperty("id").GetValue(data, null));
            idhospit = (int)id;

            GPL.Bo.Models.Log log = new Bo.Models.Log();
            log.int_id = SessionHelper.IntervenantId;
            log.log_action = filterContext.ActionDescriptor.ActionName;
            log.log_controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            log.log_date = filterContext.HttpContext.Timestamp;
            log.hospit_id = idhospit;
            log.log_libelle = Libelle;
            log.log_type = Type.ToString();
            GPL.Services.ServiceLog SvcLog = new GPL.Services.ServiceLog(Helpers.APP_CODE, SessionHelper.SiteCode, SessionHelper.IntervenantId);
            SvcLog.AjouterLog(log);
        }

        base.OnActionExecuted(filterContext);
    }
}
我的问题是PartialViewResult类型。我在TempData/ViewData/ViewBag中保存了“id”值,但在LogFilter中它始终为null

    [LogFilter(Type = TypeLog.Ajout, Libelle = "Ajout/Modification hospitalisation")]
    public ActionResult AjoutHospitalisation(string ufsCode, DateTime dateDebutCalendrier, DateTime dateFinCalendrier, TypeView viewType,
        GPL_Hospitalisation model, FormCollection collection)
    {
            //save or update and bussiness logic ....

            //then return view
            AgendaController ac = new AgendaController();
            TempData["id"] = t.Item3;
            ViewData["id"] = t.Item3;
            ViewBag.id = t.Item3;
            //Ask another controller to send PatialViewResult
            return ac.Jour(ufsCode, dateDebutCalendrier);
    }
那么,在actionFilter中发送一些参数的好方法是什么?
谢谢您的帮助。

如有评论中给出的答案,我将替换

    if (retour != null && retour is PartialViewResult)
    {
        PartialViewResult pvr = retour as PartialViewResult;
        data = new { Success = true, id = pvr.ViewData["id"] };
    }
由此

    if (retour != null && retour is PartialViewResult)
    {
        data = new { Success = true, id = filterContext.Controller.TempData["id"] };
    }

它很有效,我在TempData中检索到了很好的价值。

感谢您提供的链接,我没有通过谷歌找到:/