0

I have created a partial view to save email from users. The partial view is page location is under shared folder. when the partial view is rendered inside any view under views folder it works fine. That is in about page , contact page etc. But the problem occurs when I place the partial view in footer of _Layout page. The partial view is accepting the email and saving the data but when it returns it gives an error.

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Test.Models.FAQ]', but this dictionary requires a model item of type 'Test.Models.Subscribes'.

I tried to using Render and Partial views

@{ Html.RenderPartial("_Sub"); } and @Html.Partial("_Sub")

I also tried to add parameters but will get same error.

@{ Html.RenderPartial("_Sub",ViewData.Models.Subscribe); } and @Html.Partial("_Sub", model :Model)

This partial view not let me render other view also giving the same error. The view is performing validation and saving data to data base.

How can I resolve this error. Or is it not possible to use Partial view with models?

  • Check this question: [link](http://stackoverflow.com/questions/2775860/how-to-pass-model-from-a-view-to-a-partial-view) –  May 12 '16 at 16:22
  • The partial view is displaying a create from to accept email. How can I use view data for the forms? – abhilash d.rai May 12 '16 at 16:30
  • The message is self explanatory. The model in the main view is `List` and your passing that to your partial which has `@model Test.Models.Subscribes`. You need to pass an instance of `Subscribes` to the partial - for example `@{ Html.RenderPartial("_Sub", new Subscribes()); }` –  May 12 '16 at 22:55

1 Answers1

1

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Test.Models.FAQ]', but this dictionary requires a model item of type 'Test.Models.Subscribes'.

It seems that You are passing 2 different models into one strongly typed view , when You pass partial with model to layout it "will meet" another model from generated view. To prevent it try (for e.g.) : wrap Your models into model wrapper ,or try to use tuple, for more please check Multiple models in a view

Community
  • 1
  • 1
joint_ops
  • 312
  • 1
  • 5
  • 20