0
votes

I have a partial view: UnderAge.cshtml I'm getting a list of Artist(a model class) in that partial view. I have an Index parent view, which displays a list of Person(other model class), where I want to add the content from the Partial View UnderAge.cshtml. I've checked that the partial view works good and it has a list of two elements, the problem is when I try to show the list of Artist from partial view UnderAge.cshtml on Index parent view, maybe because Index is using elements from other model class (Person).

Partial View(I'm putting just the @model part), it display a list of Artist model class:

@model IEnumerable<AlexMusicStore.Models.Artist>

Parent view(has a different @model) And I'm getting this exception:

The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[AlexMusicStore.Models.Person]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1[AlexMusicStore.Models.Artist]'.

@model IEnumerable<AlexMusicStore.Models.Person>

<div>
@{ 
    Html.RenderPartial("~/Views/Artists/UnderAge.cshtml", Model);
}
    </div>
1
The error is self explanatory. Your passing Model (which is a collection of Person to a view which expects a collection of Artistuser3559349
Change to this line Html.RenderPartial("~/Views/Artists/UnderAge.cshtml", Model.Artist); So assuming your Model has a property called Artist and it is of type IEnumerable<AlexMusicStore.Models.Artist>Rajshekar Reddy
@Reddy, The model is IEnumerable<Person> - it cant possibly have a property which is Artist :)user3559349
@Reddy I'll try with RenderAction and I'll let you know, thanks for your suggestionAlexGH
@Reddy It worked good with RenderAction.. Thanks!!AlexGH

1 Answers

0
votes

Try this,

@model IEnumerable<AlexMusicStore.Models.Artist>

<div>

    @Html.Partial("UnderAge"));

</div>

OR

Also you can create model like this,

 public class Person
    {      
          public IEnumerable<ArtistModel> Artists{ get;  set;} //Passed to Partial
    }

    public class ArtistModel
    {      
           //your values here
    }

then display like this in Parent View

@model IEnumerable<AlexMusicStore.Models.Person>

<div>
@{ 
    Html.RenderPartial("~/Views/Artists/UnderAge.cshtml", Model.Artists);
}
</div>