简体   繁体   中英

How to return partialView in OnActionExecutiong?

Im a bit confused if some session variable is empty i want to result in to a partial view instead of redirectResult. Is it possible? if so how? i can see that there are a PartialViewResult, but i cant figure out how to use it.

 public class XXActionFilter : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext context = HttpContext.Current;

            if ((context.Session[item] == null))
            {
                filterContext.Result = my partial view
                return;              
            }


            base.OnActionExecuting(filterContext);
        }
    }

You should just pass the instance of PartialViewResult to the Result property:

public class XXActionFilter : ActionFilterAttribute, IActionFilter
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext context = HttpContext.Current;

        if ((context.Session[item] == null))
        {
            var result = new PartialViewResult
            {
                 ViewName = "PathToView"
            };

            filterContext.Result = result;
            return;              
        }


        base.OnActionExecuting(filterContext);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM