In ASP.NET MVC I encounter an incorrect type error when rendering a user control with the correct typed object

StackOverflow https://stackoverflow.com/questions/5544

  •  08-06-2019
  •  | 
  •  

Question

I encounter an error of the form: "The model item passed into the dictionary is of type FooViewData but this dictionary requires a model item of type bar" even though I am passing in an object of the correct type (bar) for the typed user control.

Was it helpful?

Solution

What @MattMitchell said is probably the reason you're seeing this error.

If you want to know why; it is because when you pass null as the controlData parameter when using RenderUserControl(), the framework will try to pass the view data from the current view context onto the user control instead (see UserControlExtensions.DoRendering method in System.Web.Mvc).

OTHER TIPS

What has probably happened is that the object provided when rendering the user control is actually null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top