Search code examples
overridingviewbag

How to set ViewBag in IActionFilter MVC6?


public class MAttribute : IActionFilter
{

public void OnActionExecuted(ActionExecutedContext actionContext)
    {
        string searchData = "";
        actionContext.Controller.ViewBag.SearchItem = searchData;
        //object not included in the definition
    }

}

when i override IActionFilter , it prompt 'object not included in the definition', so How to set ViewBag in IActionFilter MVC6 ?


Solution

  • Try inheriting from ActionFilterAttribute And I think the method signature you want is OnActionExecuting(ActionExecutingContext)

    For more info, see: http://www.strathweb.com/2015/06/action-filters-service-filters-type-filters-asp-net-5-mvc-6/

    Edit: You'll probably also want to add using Microsoft.AspNetCore.Mvc.Filters; at the top if you haven't already.