Hi!
I'm trying to return a different view if a condition is met. I want to preserve the Model passed into the view from the action.
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var subAction = filterContext.RequestContext.RouteData.Values["subaction"].ToString();
var action = filterContext.RequestContext.RouteData.Values["action"].ToString();
if (!string.IsNullOrEmpty(subAction))
{
var view = (ViewResultBase) (filterContext.Result);
filterContext.Result = View(action + subAction, view.ViewData.Model);
}
base.OnActionExecuting(filterContext);
}
When doing it this way I get an "Object reference not set to an instance of an object.
". Obviously, the Model haven't been set?
The reason I'm doing it this way, is that I want to keep the naming of the view as simple as possible. And my URL looks like this: /Global/Modules/Admin/Users/Create
. That would return the view "UsersCreate". Which works. But the Model is either empty or null!
UPDATE
Actually. It just hit me. The behaviour is correct. Because I'm only returning a View. The action is never executed. The Users-action gets executed, but returns the view UsersCreate. How can I do a RedirectToAction kinda thing, without actually redirect.