General Electric
General Electric

Reputation: 1226

Passing model to partial View

I'm new to ASP.NET MVC, so far I have read that I have if I have lets say two form in one view, the recommended thing to do, is to have a View model which will have the model associate for each form, this forms are not related in any way just are in the home page, one form is for searching a fly and the other is contact form so as you can see there is no relationship of any kind.

this my "HomePageViewModel"

public class HomePageViewModel
    {
        public SearchFlyViewModel SearchFly;
        public ContactFormViewModel Contact;        
    }

, in my index file which is the homepage I have this at the top

@model Project.WebSite.Models.HomePageViewModel

now, I have a partial view inside my homepage, this partial view has the search fly form, I did it this way because I use the same form in a lot of places.

My partial view is _QuoteForm in which i have this at the top

@model Project.WebSite.Models.SearchFlyViewModel

when I tried to do this in my homepage

@Html.Partial("_QuoteForm", Model.SearchFly)

an error is showing up in runtime telling me this

The model item passed into the dictionary is of type 'Project.WebSite.Models.HomePageViewModel', but this dictionary requires a model item of type 'Project.WebSite.Models.SearchFlyViewModel'.

Which I don't understand why because I did not pass the HomePageViewModel to my partial view, I passed Model.SearchFly which is of the type SearchFlyViewModel

if someone can help figure it out what I'm missing.

thanks

Upvotes: 1

Views: 683

Answers (1)

Mate
Mate

Reputation: 5274

The second parameter ( "The model" ) of the Html.Partial should not be null.

@Html.Partial("_QuoteForm", Model.SearchFly)

Verify

Model.SearchFly != null 

Upvotes: 1

Related Questions