Tuesday, May 26, 2015

ASP.NET MVC:How to pass the object from Filter to Controller method

     Consider request caching for persist the values on each http request. So store the object  in                       HTTPContext.item  and read the object from  HTTPContext.Item.  
Make sure that store  necessary properties on view in hidden fields . ASP.Net model binding will construct the  object from the view so that we no need to re build the object every time.

<![if !supportLists]>1.       <![endif]> On filter store the values
public override void OnAuthorization(AuthorizationContext filterContext)
{     
UserViewModel userviewModel =  new UserViewModel()
//load the properties using some service…           
filterContext.RequestContext.HttpContext.Items["UserViewModel"] = object;
}
<![if !supportLists]>2.  <![endif]>On controller read the object from HTTPContext
UserViewModel userviewModel =    HttpContext.Items["UserViewModel"as UserViewModel

No comments:

Post a Comment