1

I'm getting the following error when rendering a partial view and I'm not sure why as I'm passing through the pagination property as my model.

The model item passed into the dictionary is of type 'StockManager.ViewModels.StockManagementViewModel', but this dictionary requires a model item of type 'StockManager.ViewModels.Shared.PaginationViewModel'.

Here's the model...

public class StockManagementViewModel : ViewModelBase
{
    public List<StockReportParentRowsViewModel> StockLevels { get; set; } = new List<StockReportParentRowsViewModel>();

    public PaginationViewModel StockLevelsPagination { get; set; }

    public Dictionary<Int32, String> Full_Site_List { get; set; } = HelperMethods.GenericHelpers.GetFullSiteList();
}

And here is where I pass PaginationViewModel instance into the partial view:

 @{ Html.RenderPartial("~/Views/Shared/_PaginationPartial.cshtml", Model.StockLevelsPagination); }

Finally, here's the view model reference for the pagination view.

@model StockManager.ViewModels.Shared.PaginationViewModel

StockManagementViewModel.StockLevelsPagination is definitely an instance of StockManager.ViewModels.Shared.PaginationViewModel so what gives? What have I missed?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sean T
  • 2,414
  • 2
  • 17
  • 23
  • 2
    You are passing Model.StockLevelsPagination. Where do you initialize this variable? – derloopkat Jan 07 '18 at 14:49
  • That's it, the test case I'm using doesn't reach the constructor for it, thanks. That's not as helpful error message, I'd have thought it would say null reference or something instead. – Sean T Jan 07 '18 at 14:55

1 Answers1

1

Usually, this happens if the value you're passing is null. So if the property X.Y is null, it just gives up and passes X, then throws this exception.

Adam Brown
  • 1,667
  • 7
  • 9