ASP.NET MVC Pass object from Custom Action Filter to Action

后端 未结 4 2062
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 20:14

If I create an object in a Custom Action Filter in ASP.NET MVC in

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    Detached         


        
相关标签:
4条回答
  • 2020-11-30 20:39

    I would recommend putting it in the Route data.

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.RouteData.Values.Add("test", "TESTING");
            base.OnActionExecuting(filterContext);
        }
    
        public ActionResult Index()
        {
            ViewData["Message"] = RouteData.Values["test"];
    
            return View();
        }
    
    0 讨论(0)
  • 2020-11-30 20:40

    Set item in ViewData or of a viewmodel if you pass it as a parameter into your action. Here I set the property of a ViewModel

    public override void OnActionExecuting(ActionExecutingContext filterContext)
     {
         ViewModelBase viewModel = null;
         foreach (object parameter in filterContext.ActionParameters.Values)
         {
             if (parameter is ViewModelBase)
             {
                 viewModel = (ViewModelBase)parameter;
                 break;
             }
         }
         if(viewModel !=null)
         {
             viewModel.SomeProperty = "SomeValue";
         }
     }
    
    
        public ActionResult About(ViewModelBase model)
        {
          string someProperty= model.SomeProperty;
    }
    

    Here is the untyped version I think you prefer:

       public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewData.Add("TestValue", "test");
    
        }
    
           [FilterWhichSetsValue]
            public ActionResult About()
            {
                string test = (string)ViewData["TestValue"];
                return View();
            }
    
    0 讨论(0)
  • 2020-11-30 20:51

    You could use the HttpContext:

    filterContext.HttpContext.Items["criteria"] = criteria;
    

    And you can read it in the action:

    [YourActionFilter]
    public ActionResult SomeAction() 
    {
        var criteria = HttpContext.Items["criteria"] as DetachedCriteria;
    }
    
    0 讨论(0)
  • 2020-11-30 21:04

    The better approach is described by Phil Haack.

    Basically this is what you do:

    public class AddActionParameterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
    
            // Create integer parameter.
            filterContext.ActionParameters["number"] = 123;
    
            // Create object parameter.
            filterContext.ActionParameters["person"] = new Person("John", "Smith");
        }
    }
    

    The only gotcha is that if you are creating object parameters, then your class (in this case Person) must have a default constructor, otherwise you will get an exception.

    Here's how you'd use the above filter:

    [AddActionParameter]
    public ActionResult Index(int number, Person person)
    {
        // Now you can use number and person variables.
        return View();
    }
    
    0 讨论(0)
提交回复
热议问题