Petter Pettersson
Petter Pettersson

Reputation: 377

Wrong dictionary type when trying to pass partial view

I'm new to MVC and need some help. I created a controller called SideBarController, a viewmodel called SideBarViewModel and a view called SideBar. I want to use SideBar inside my _Layout.cshtml file so it's avaliable on all my pages.

I get this error mesage:

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

My SideBarViewModel looks like this:
using System.Collections.Generic;

namespace Learn2MVC.Models
{
    public class SideBarViewModel
    {
        public IEnumerable<BlogCategories> Categories { get; set; }
        public IEnumerable<BlogPosts> LatestPosts { get; set; } 
    }
}

It holds BlogCategories and BlogPosts inside, which are two other classes which only get;set; properties.
SideBarController looks like this:

public ActionResult SideBar()
{
    var model = new SideBarViewModel();
    model.Categories = GetCategories();
    model.LatestPosts = LatestPosts();
    return View(model);
}

//Method for getting the categories
public IEnumerable<BlogCategories> GetCategories()
{
    var categoryList = new List<BlogCategories>();
    using (var connection = new DatabaseClass().ConnectToDatabase())
    using (OleDbCommand command = connection.CreateCommand())
    {
        command.CommandText = "SELECT * FROM Categories ORDER BY CategoryName ASC";
        OleDbDataReader reader = command.ExecuteReader();
        while (reader.HasRows && reader.Read())
        {
            categoryList.Add(new BlogCategories()
            {
                Id = reader.GetInt32(0),
                Name = reader.GetString(1)
            });
        }
        if (!reader.HasRows)
            categoryList.Add(new BlogCategories(){ Id = -1,Name = "Inga kategorier hittades"});
        return categoryList;
    }
}

LatestPosts essentially work the same way as GetCategories. The important code in SideBar.cshtml looks like this:

@model Learn2MVC.Models.SideBarViewModel
            @foreach (var category in Model.Categories)
            {
                <li><a href="#">@category.Name</a></li>
                <li>@category.Id</li>
            }
            @foreach (var post in Model.LatestPosts)
            {
                <li><a href="#">@post.Title</a></li>
            }

And in my _Layout.cshtml I try to include SideBar using

@{ Html.RenderPartial("../SideBar/SideBar"); }

However, it does not work. Except for when I go to localhost/SideBar/SideBar - Then loops work just fine and no error messages popup. However, when I go to my index page I get the error message. I have no clue what could be wrong, can you guys help me?

Upvotes: 1

Views: 107

Answers (1)

scartag
scartag

Reputation: 17680

You are calling Html.RenderPartial and the view expects a model and since none is passed it is probably using the one defined in the parent (Index in this case).

You should use @Html.Action instead.

@Html.Action("SideBar","SideBar")

or

@Html.RenderAction("SideBar","SideBar")

Update

For the error you are getting add this to your SideBar.cshtml at the top.

@{
    Layout = null;
}

Upvotes: 3

Related Questions