8

I trying to return a ViewResult in OnActionExecuted method override from ActionFilterAttribute class

Like below ...

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    if (CreateCookie && filterContext.Exception == null)
    {
        LoginCookies lcookie = new LoginCookies(usuDs, usuSenha);
        lcookie.WriteCookie("SCE", 10);
    }
    else
    {
        filterContext.Result = new ViewResult() { ViewName = "Login" };
        filterContext.Result.ExecuteResult
                (filterContext.Controller.ControllerContext);
    }

It works fine to return to a view called "Login",but i need to pass the model object to this view (in this case model object is type of Users) and I don't know how to pass it using ViewResult class directly.

Any ideas?

UPDATED: I've solved my problem setting filterContext.ExceptionHandled to TRUE,but the main problem was not solved, I can't set Model property of View, it is always null.

2
  • Please show the code that doesn't work. Commented Aug 26, 2010 at 15:51
  • The code is above.When i return ViewResult Login in code above,i need to pass the model to View as well,else view throws an exception cause Model is null.Problem is i cant set the model to this view. Commented Aug 26, 2010 at 17:37

3 Answers 3

10

I ran into the same issue where my Model being passed to the View was always NULL. I was able to pass the Model to my view with the following:

Create your viewModel and set the properties. Create a new ViewResult giving it the name of your View and then pass your viewModel into the ViewData.

public override void OnActionExecuted(ActionExecutedContext filterContext)
{

    TestViewModel viewModel = new TestViewModel; 

    //Here set all the properties of your viewModel such as your exception message

    filterContext.Controller.ViewData.Model = viewModel;
    filterContext.Result = new ViewResult { ViewName = "Login", ViewData = new ViewDataDictionary(viewModel)};
    filterContext.ExceptionHandled = true;

}
Sign up to request clarification or add additional context in comments.

Comments

2

I may be mistaken, but I believe that the view data is part of the controller base and not actually part of the view itself. So you should be able to set the view data by doing such:

filterContext.Controller.ViewData.Model = <your view model>

I just tested and this worked for me. I don't see any reason why it shouldn't work for you:

public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        TestClass1 viewModel = new TestClass1();

        viewModel.FirstName = "TestFilter";

        filterContext.Controller.ViewData.Model = viewModel;
    }

Referencing documentation

4 Comments

wow,i thought it works,but nope,that Model is always null in the view
@ozsenegal In your example you are in the OnActionExecuted event, but are you sure you are in it in your actual code? Your viewdata should be accessible at this point.
yes,im sure.View data is acessible the way you show,but in the view the model property keeps null
@ozsenegal check my update. My code worked for me, it changed the view model to what I added in the filter.
2

Maybe this will work for you:

filterContext.Result = new ViewResult { ViewName = "Exception", ViewData = new ViewDataDictionary(new CmsExceptionViewData(filterContext.Exception, action, controllerName, errorMessage)) };

So the ViewData is created with a ViewDataDictionary which accepts either a dictionary or a model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.