0

I've got an MVC project with a Layout that has strongly typed model named "Dashboard".

@model Dashboard_v2.Models.Dashboard

This Layout has a RenderBody() method that renders the rest of the pages.

then, on my main page, I have a View that is strongly typed with the Model NewPatient.

on the controller I have bunch of code, and eventually I pass a NewPatient Model to it.

When I try to run this code, I've got an error called :

The model item passed into the dictionary is of type 'Dashboard_v2.Models.NewPatient', but this dictionary requires a model item of type 'Dashboard_v2.Models.Dashboard'.

why does it expects a Dashboard Model, if my page has strongly typed NewPatient Model?

tereško
  • 58,060
  • 25
  • 98
  • 150
thormayer
  • 1,070
  • 6
  • 28
  • 49
  • Why do you need a `Model` on a layout `View` anyway? – emerson.marini May 05 '15 at 12:31
  • 2
    Remove the model definition from the layout. The model needs to be defined in the view (you are telling every view that its model must be `Dashboard`) –  May 05 '15 at 12:31
  • 2
    Optionally (i.e. _it's a must_), read [this](http://stackoverflow.com/questions/13225315/pass-data-to-layout-that-are-common-to-all-pages). – Andrei V May 05 '15 at 12:32
  • If your wanting to render some details relating to `Dashboard` in all views, then in the layout use `@Html.Action()` to call a child action method that returns a partial view based on an instance `Dashboard` –  May 05 '15 at 12:34

1 Answers1

0

One approach is to define a Viewmodel with generic parameter MainViewModel<T>and instance it with appreciated T for the views. Then you can use the MainVewModel properties in Layout and use properties of T in your views.

Mike Anderson
  • 738
  • 1
  • 8
  • 23