0

I'm creating a search page. The main page will display the search results with a sidebar that contains the search menu controls, rendered as a partial view. Both the main view and partial view will have different view models. See code below, which is generating the following error:

The model item passed into the dictionary is of type 'SearchResultsViewModel', but this dictionary requires a model item of type 'SearchMenuViewModel'

Index.cshtml

@model SearchResultsViewModel

<div class="sidebar">     
    @Html.Partial("_SearchMenu", Model)
    @{Html.RenderAction("SearchMenu", "Search");}
</div>

<div class="results">
@foreach(var result in searchResults)
{
    //display search results
}
</div>

_SearchMenu.cshtml

@model SearchMenuViewModel

@using (Html.BeginForm("Index", "Search", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    //Search parameter controls
}

SearchController

public ActionResult SearchMenu()
{
    var model = _searchRepository.GetSearchMenu();
    return PartialView("_SearchMenu", model);
}

public ActionResult Index()
{
    var model = _searchRepository.GetSearchResults();
    return View(model);
}
PixelPaul
  • 2,609
  • 4
  • 39
  • 70
  • 1
    I think your method _searchRepository.GetSearchMenu(); returns SearchResultsViewModel rather than SearchMenuViewModel – Thiago Custodio Mar 08 '17 at 01:37
  • See the section of the dupe titled _Passing the wrong model from a view to a partial view_ (your `SearchResultsViewModel` needs to contain a property that is typeof `SearchMenuViewModel`, and you pass that property to the partial) –  Mar 08 '17 at 01:40
  • Thanks @Stephen Muecke. My problem was indeed with my models, the `SearchMenuViewModel` was not a property of `SearchResultsViewModel`. – PixelPaul Mar 08 '17 at 17:08

0 Answers0