Reputation: 6326
I have a partial view in which I have a model specified as such:
@model IEnumerable<AbstractThinking2015.Models.BlogModel>
@foreach (var item in Model.Take(5))
{
<li>@Html.ActionLink(item.Title, "Details", new { id = item.BlogModelId })</li>
}
And I'm calling this using:
<div class="widget">
<h4>Recent Posts</h4>
<ul>
@Html.Partial("View")
</ul>
</div>
But I'm getting the error:
The model item passed into the dictionary is of type 'AbstractThinking2015.Models.BlogModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[AbstractThinking2015.Models.BlogModel]'.
I'm sure this is because the model being passed into the view is the single blog but I want to use the list that's defined in the partial view. Is there a way to do this?
Upvotes: 0
Views: 1039
Reputation: 218852
If you do not pass a model t the partial explicitly, It will take the model of the parent view. So from your error message, It is clear that you are passing a single object of the BlogModel to your view from your action method, hence getting the error.
You need to make sure that your main view (where you are calling the partial), is also strongly typed to a collection of BlogModel
objects.
public ActionResult Index()
{
var blogList=new List<Models.BlogModel>();
// or you may read from db and load the blogList variable
return View(blogList);
}
And the Index view, where you are calling the Partial will be strongly typed to a collection of BlogModel
.
@model List<Models.BlogModel>
<h1>Index page which will call Partial</h1>
<div class="widget">
<h4>Recent Posts</h4>
<ul>
@Html.Partial("View")
</ul>
</div>
Upvotes: 4