Dheeraj Palagiri Dheeraj Palagiri - 9 months ago 84
C# Question

Passing Glass Model in sitecore view rendering

We have two glass views inherited from different Glass models, both working great individually.

Now, We want to insert one view into another. So I tried using the code below:

var model = new SitecoreContext().GetItem<IOurGlassModel>(path);
if (model != null)
@Html.Sitecore().ViewRendering("/Views/path/Banner.cshtml", new { Model = model })

This ended up with below error message:

Server Error in '/' Application. Could not locate item containing
model definition. Model path: Castle.Proxies.IOurGlassModelProxy_1

Let me know if you need the full stack trace.

Any suggestions will be appreciated.

Answer Source

Use this insted:

@Html.Partial("/Views/path/Banner.cshtml", model)

The point is @Html.Sitecore().ViewRendering will re-invoke Sitecore pipelines and render your component from the begging. on the other side, using @Html.Partial will render the partial view using the same execution.

check this question for more details about the difference between the two methods: Sitecore View Rendering and Controller Rendering Helper