Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC return ViewResult

Tags:

c#

asp.net-mvc

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.

like image 630
ozsenegal Avatar asked Aug 25 '10 23:08

ozsenegal


3 Answers

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;

}
like image 194
tokyovariable Avatar answered Nov 09 '22 18:11

tokyovariable


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

like image 44
joshlrogers Avatar answered Nov 09 '22 17:11

joshlrogers


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.

like image 2
Michiel Avatar answered Nov 09 '22 16:11

Michiel