Last post May 14, 2020 07:22 AM by HoomanBahreini
May 11, 2020 07:11 AM|HoomanBahreini|LINK
I have created the following Custom Action Filter, when I try to access the Model in the following code, it is null:
public class CustomPermissionCheckAttribute : ActionFilterAttribute
public override void OnActionExecuting(ActionExecutingContext context)
OrganisationBaseController orgBaseController = context.Controller as Controller;
MyViewModel m = ((Controller)context.Controller).ViewData.Model as MyViewModel; // null
// check if current user has permission to m.OrganisationId
I am trying to understand why the Model is null? According to ASP.NET MVC Pipeline, Action
Filters are executed after Model Binding, so I am not sure why the Model is not available?
This is how I am register the above Action Filter:
public ActionResult UpdateBranch(MyViewModel myViewModel)
// do something...
May 11, 2020 02:18 PM|bruce (sqlwork.com)|LINK
The viewdata.model, is set when the viewcontext is setup via
May 12, 2020 09:39 AM|YihuiSun|LINK
You can get your model in the following way.
MyViewModel m = context.ActionParameters.Values.SingleOrDefault() as MyViewModel;
Here is the result.
May 14, 2020 07:22 AM|HoomanBahreini|LINK
Thanks a lot,