I am fairly new to MVC and getting what seems to be a fairly common error but can't seem to find a solution to my particular case. A partial view is causing this error when I send it a model as so:
@model Webcasting.Library.Models.StaticPageListModel
<div class="nav-main_small-screen collapse" id="navSmallScreen">
<ul>
@for (var i = 0; i < Model.Pages.Count; i++)
{
<li>@Html.DisplayFor(m => Model.Pages[i].sFriendlyUrl)</li>
}
</ul>
</div>
Rather than receiving a model of this type it is getting sent the model of the parent view. The partial view is added to _Layout.cshtml as so and included in all pages:
@Html.Partial("_Nav")
The model:
namespace Webcasting.Library.Models
{
public class StaticPageListModel : MasterModel
{
public List<StaticPage> Pages { get; set; }
public StaticPageListModel()
{
Pages = StaticPage.GetAllPages();
}
}
}
The controller is as follows:
namespace Webcasting.Library.Controllers
{
public class StaticPageListController : MasterController
{
public ActionResult Page()
{
return View(new StaticPageListModel());
}
}
}
I have looked at this post: The model item passed into the dictionary is of type .. but this dictionary requires a model item of type And this looked promising:
@Html.Partial("_Bar", new Bar())
However, the cshtml page is unable to reference the Bar() file. Without making an instance of this model in the Main view model class how can I make an instance of Bar and pass this to the partial view?