Sam Huggill
Sam Huggill

Reputation: 3126

Use different ViewModel for ASP.NET MVC 3 global error page

I'm using a global filter to handle errors in ASP.NET MVC 3 (with Razor views). However, my shared/_Layout view requires a view model which I've called PageViewModel.

When I hit an error the shared/Error view gets compiled - however because it is referencing the layout file I'm getting this exception:

Exception message: The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type 'ViewModels.PageViewModel'.

Any idea of how to solve this?

Here's the error view:

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<h2>Sorry, an error occurred while processing your request.</h2>

Upvotes: 0

Views: 1336

Answers (1)

Zach Green
Zach Green

Reputation: 3460

Couldn't you just put the error into the ViewBag, and just send the view the normal PageViewModel?

Here is good example of global exception handling in MVC, How can I properly handle 404 in ASP.NET MVC?.

Upvotes: 2

Related Questions