0

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack trace:

InvalidOperationException: The model item passed into the dictionary is of type 'Name.Models.IndexViewModel', but this dictionary requires a model item of type 'Name.Models.TwoViewModel'.

I needed two different view models for this view:

public class TwoViewModel
{
    public IEnumerable<Name.Models.Project> Model1 { get; set; }
    public IndexViewModel Model2 { get; set; }
}

EDIT:

I think I have found where the problems come from, the ManageController:

public async Task<ActionResult> Index(ManageMessageId? message)
{
    var userId = User.Identity.GetUserId();
    var userEmail = User.Identity.Name;
    var user = UserManager.FindById(userId);
    var model = new IndexViewModel
    {              
        Email = user.Email,
        FirstName = user.FirstName,
        LastName = user.LastName,
        City = user.City,            
    };    
    return View(model);
}

I have tried changing the ViewModel of the model, trying things like

var model = new TwoViewModel
{
    Model2.Email = user.Email,
    Model2.FirstName = uesr.FirstName,
    Model2.LastName = user.LastName,
    Model2.City = user.City,                       
};             
return View(model);

And intellisense does recognize the Model2 but not the .Email part. I think a nesting would be the case?

I've tried this approach:

var model = new TwoViewModel
{
    Model2 = new IndexViewModel
    {
        Email = user.Email,
        FirstName = user.FirstName,
        LastName = user.LastName,
        City = user.City,
    }
};    
return View(model);

But then another thing gives an

Object reference not set to an instance of an object.

error.

@foreach (var item in Model.Model1)
{
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Title)</td>
        ....
    </tr>
}

This @foreach (var item in Model.Model1) line is the cause of the problem. Now the syntax is good. I have this same line that will retrieve everything from the first model (IEnumerable<Name.Models.Project>) with no problems. I don't know how to deal with this error

EDIT: the solution was that I had to initialize Model1 inside the model of type TwoViewModel inside the ManageController. This fixed my error

crystyxn
  • 1,411
  • 4
  • 27
  • 59
  • [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) –  Apr 30 '16 at 10:16
  • The error message is self explanatory. You have a view with `@model TwoViewModel` but somewhere you pass a model to that view which is `IndexViewModel` –  Apr 30 '16 at 10:18
  • Alright I'll add all the code then to be able to reproduce the problem – crystyxn Apr 30 '16 at 10:19
  • No, what you need to do is delete 99% of this code - it has absolutely nothing to do with the problem –  Apr 30 '16 at 10:20
  • You have not initialized the property `Model1` of `TwoViewModel` so its `null` (you cannot iterate over something which is null, hence the exception) –  Apr 30 '16 at 11:24
  • I also realized this after debugging. How exactly would I go about initializing properly? It gave no syntax errors so I assumed this is the way it is done? – crystyxn Apr 30 '16 at 11:35
  • 1
    You can use `var model = new TwoViewModel { Model2 = new IndexViewModel { ..as above.. }, Model1 = new List }` or initialize it in a constructor for `TwoViewModel` –  Apr 30 '16 at 11:38
  • This got rid of the error, thank you! gonna edit the OP – crystyxn Apr 30 '16 at 11:58
  • 1
    Don't edit the question. Add an answer :) –  Apr 30 '16 at 11:59
  • Alright did just that :) – crystyxn Apr 30 '16 at 12:03

1 Answers1

0

The thing causing the problem was that the Model 1 was not initialized and so it was a NULL.

The solution was that I had to initialize Model1 inside the model of type TwoViewModel inside the ManageController. This fixed my error

crystyxn
  • 1,411
  • 4
  • 27
  • 59