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);
}