Monday, December 14, 2015

Sitecore MVC: Static rendering issue on layout

MVC is not my best strength, so I'm not sure if this was just one of those easy errors where "you should just know better", but I wasn't able to find much about this error online.

"Could not locate item containing model definition. Model path: My.Namespace.WebsiteModel"

I was creating a rather simple rendering to put in the footer of my layout, that would grab a single line text from the root item of my website. I referenced the rendering using the following code...

@Html.Sitecore().ViewRendering("~/Areas/Views/MyRendering.cshtml", new { Model = new WebsiteModel() });

Which gave me the above error. It sounds like it was looking for a Model item inside of Sitecore. But since this rendering is being used statically, I shouldn't have to create anything in Sitecore for it to work.

The following code ended up working...

@{ Html.RenderPartial("~/Areas/Views/MyRendering.cshtml", new WebsiteModel()); }
My homework now is to dig into @Html.Sitecore().ViewRendering() and see what exactly it's trying to do under the covers. I'm guessing it's more meant to be used with a Sitecore rendering item, and since I was trying to use a custom Model, it was looking for the Model item that would exist if it *was* a Sitecore item.