ActionFilter OnActionExecuted and TempData

本文关键字:TempData and OnActionExecuted ActionFilter | 更新日期: 2023-09-27 17:57:49

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

然后我创建了一个LogFilter,将其用作方法属性

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中它一直为空。

    [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中发送一些参数的好方法是什么呢?谢谢你的帮助。

ActionFilter OnActionExecuted and TempData

根据注释中给出的答案,我替换

    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中检索到了良好的值。