0

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>
Sam Huggill
  • 3,106
  • 3
  • 29
  • 34
  • Are you using the HandleErrorInfo model in the Error view? – Dallas Nov 21 '11 at 13:38
  • No, the entire view is posted above. – Sam Huggill Nov 21 '11 at 13:46
  • If you are not using any values from the HandleErrorInfo, does the error view need to be strongly typed? Alternatively, does the layout need to be strongly typed? Won't this cause the same problem for any other strongly type views? – Dallas Nov 21 '11 at 13:51
  • The strongly typed layout is required so I can drop site settings etc into the standard template. I suppose I could do this differently by using child actions. – Sam Huggill Nov 22 '11 at 08:56

1 Answers1

2

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?.

Community
  • 1
  • 1
Zach Green
  • 3,421
  • 4
  • 29
  • 32
  • I'm not specifically calling the view - that's happening from within ASP.NET somewhere - do you know how to override this so I can change the view model that gets sent? – Sam Huggill Nov 21 '11 at 14:32
  • Here is good example of global exception handling in MVC, http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc. You should probably going down this path to specifically call your error view. I am with Dallas though that having a strongly typed layout is probably going to give you other issues down the line. – Zach Green Nov 21 '11 at 14:39