0

I'm trying to pass some data into my Layout, but can't get it working. I've succesfully done this before, but this time, no matter what I'm trying, it just doesn't work.

I'm getting this error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[DOOR.Core.Web.Models.DataTaxonomy.Object]', but this ViewDataDictionary instance requires a model item of type 'DOOR.Core.Web.Pages.DataTaxonomyTool.IndexModel'.

Anyone dealt with something similar in the past?

My Default.cshtml:

@model DOOR.Core.Web.Pages.DataTaxonomyTool.IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = "~/Pages/DataTaxonomyTool/Layout/LayoutTaxonomy.cshtml";
}   
@for (char c = 'A'; c <= 'Z'; c++)
{
    <ul style="list-style:none">
        <li class="nav-item">
            <a class="nav-link" data-toggle="collapse" data-target="#@c.ToString().ToLower()">@c</a>
            <div id="@c.ToString().ToLower()" class="collapse">
                @foreach (var item in Model.Object.OrderBy(x => x.ObjectName))
                {
                    @if (item.ObjectName.StartsWith(c.ToString().ToLower()))
                    {
                        <ul style="list-style:none">
                            <li>@item.ObjectName</li>
                        </ul>
                    }
                }
            </div>
        </li>
    </ul>
}

My ViewComponent Class:

public class LayoutListsViewComponent : ViewComponent
    {

        DOOR.Core.Web.Models.ReportContext _context;

        public LayoutListsViewComponent(DOOR.Core.Web.Models.ReportContext context)
        {
            _context = context;
        }
        public IList<Object> objects { get; set; }
        public async Task<IViewComponentResult> InvokeAsync()
        {
            var objects = await _context.objects.ToListAsync();
            return View(objects);
        }

    }

My Layout page:

addTagHelper*,DOOR.Core.Web

<!DOCTYPE html>
<html>
...
<vc:layout-lists></vc:layout-lists>
...
Brad Patton
  • 4,007
  • 4
  • 34
  • 44
xcelm
  • 541
  • 1
  • 6
  • 19
  • 1
    Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – mjwills Feb 27 '19 at 12:27

2 Answers2

1

You are returning different object to view while your view is strongly typed with different one. Apparently looks like you forgot to send the IndexModel returned back with Object property populated.

As your view has @model DOOR.Core.Web.Pages.DataTaxonomyTool.IndexModel while what you are returning is a collection of some Object type i.e. System.Collections.Generic.List1[DOOR.Core.Web.Models.DataTaxonomy.Object]`, which obviously will not work.

You probably want following:

IndexModel model = new IndexModel();
model.Object = await _context.objects.ToListAsync();
return View(model);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
-1

We can't view the types but it's obvious that the model returned to the view is not of the type expected.

Put some breakpoints and look at what you are returning, you need to convert from that to

DOOR.Core.Web.Pages.DataTaxonomyTool.IndexModel

One more thing from me, I have serious doubts about using async when returning a view. I wouldn't do that if I were you, use it when you want to get data, but not when you are returning a view

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32