ActionFilter OnActionExecuted和TempData

时间:2014-04-11 10:58:13

标签: c# asp.net-mvc-4 action-filter

我的目标是使用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类型。我已经保存了#id; id" TempData / ViewData / ViewBag中的值,但在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中发送一些参数的好方法是什么? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

在评论中给出答案,我替换

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