0

It seems like this question has been asked and asnwered a lot on this site, but I still cannot seem to get my problem solved. I'm getting the The model item passed into the dictionary is of type 'X', but this dictionary requires a model item of type 'Y' error, even though I'm pretty sure my code is pointing to the right place and receiving the correct viewModel.

View (Index)

@model EveryNationRandburg.ViewModels.AllUsers

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Church Members</h2>

<p>@Html.ActionLink("New Member", "Register", "Account", null, new { @class = "btn btn-primary" })</p>

<table class="table table-bordered table-hover" id="kerkmembers">
    <thead>
    <tr>
        <th>Member</th>
        <th>Email</th>
        <th>Contact Number</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var member in Model.AlleKerkMembers)
    {
        <tr>
            <td>@Html.ActionLink(@member.UserName, "Edit", "KerkMember", new {id = @member.Id}, null)</td>
            <td>@member.Email</td>
            <td>@member.PhoneNumber</td>
        </tr>
    }
    </tbody>
</table>


@section scripts
{
    <script>
        $(document).ready(function (){
            $("#kerkmembers").DataTable();
        })
    </script>
}

ViewModel

public class AllUsers
{
    public List<ApplicationUser> AlleKerkMembers = new List<ApplicationUser>();
}

Account Controller

public ActionResult Index()
{
    var viewModel = new AllUsers
    {
        AlleKerkMembers = _context.Users.ToList()
    };
    return View(viewModel);
}

StackTrace

InvalidOperationException: The model item passed into the dictionary is of type 'EveryNationRandburg.ViewModels.AllUsers', but this dictionary requires a model item of type 'EveryNationRandburg.ViewModels.KonnekGroepViewModel'.

What is slightly different for my problem is that whenever I try to send a viewModel to a view, I always get the error saying it is expecting item of type 'Y' (always the same one, no matter what model I declare at the top of my view)

Code Vader
  • 739
  • 3
  • 9
  • 26
  • At which row error message is pointing to? Is it **return View(viewModel);** You could add the whole error message to problem description. – KettuJKL Feb 16 '17 at 06:34
  • 1
    try to clear the solution, restart visual studio – Dan Nguyen Feb 16 '17 at 06:41
  • The error is obvious - your passing model which is `AllUsers` to a view that expects `KonnekGroepViewModel` but you have not shown the relevant code (the view that has `KonnekGroepViewModel` or how you call it). Best guess is you using `@Html.Partial()` in the main view. –  Feb 16 '17 at 07:23
  • Hi @StephenMuecke, I've added the full view. As you can see, the model I'm expecting is AllUsers. I can step through the view and verify that I'm receiving the correct data in it – Code Vader Feb 16 '17 at 07:27
  • Again - read the error. You have not shown us the correct code. Somewhere you have a view with `@model KonnekGroepViewModel` which you are calling but are passing an instance of `AllUsers`. Read the dupe which lists the main causes. –  Feb 16 '17 at 07:28
  • @StephenMuecke, well played kind sir! Can you repost your answer as an actuall answer so that I can mark it as correct. I called a incorrect model in my Shared._Layout.cshtml file – Code Vader Feb 16 '17 at 07:33

1 Answers1

0

First thing I'd suggest is to attach a debugger to your code and see values you are handling. With debugger you can see call stack which we can't see here.

Where do you use result for return View(viewModel); ?

You don't have listing for your view's constructor.

What is the type of object your view is expecting? Should be AllUsers since you are passing AllUsers object to it.

You can try using explicit types instead of var to be sure you are passing right object type. Code would be easier to read too.

Please attach your debugger and try to solve it out that way.

KettuJKL
  • 227
  • 3
  • 14
  • 1
    Hi @KettuJKL When I debug, I can step through the entire view without any errors. I can actually see the objects that I pass to the view via the viewmodel (AllUsers) from the controller. I render a table in this view and display some details from all user in my db. Once the code steps out of the view it enter the Dispose() method of the controller. Steps through without error and then suddenly crash. I've cleaned, rebuilt, and restarted VS. No idea why this is caused. – Code Vader Feb 16 '17 at 07:07
  • Could you please add additional description to your question and the debug log? I mean that it happens in the Dispose method and crash log. Someone might see instantly what is going on. – KettuJKL Feb 16 '17 at 07:14
  • 1
    Ohke, added the stack trace of the error – Code Vader Feb 16 '17 at 07:18