2

I am trying to use @Html.Sitecore().Rendering("--id--", Model.Property) to call render a staic view. It is not rendering on page.

Layout:

@Html.Sitecore().Rendering("--Myviewid--", Model)

Myview:

@model namespace.Model
@{
    var list = Model.GetMyList();
}
foreach(var item in list)
{
    ---my logic ---
}

It executes without any exception. No errors logged in the log files. I even added a debugger to view and went through each step to see if it is breaking anywhear. Code is executed without any exception. But, view is not rendered on the page.

I also tried @Html.Sitecore().Rendering("--MyviewId--", new {Model = myModel}). @Html.RenderPartical works perfectly but can anyone let me know why Sitecore().Rendering is not working even after executing without any errors


EDIT (copied from OP's comment):

In layout there is

@if(condtion){ Html.Sitecore.Rendering }

I confirm that condition is true and the view code is executed without error.

3
  • Do you have @{...} around the Html.Sitecore.Rendering in your layout? Or is it inside some html tag like div?
    – Marek Musielak
    Jul 1, 2019 at 18:39
  • @if(condtion){Html.Sitecore.Rendering }. I confirm that condition is true and the view code is executed without error
    – Shiva
    Jul 2, 2019 at 3:51
  • "--Myviewid--" is view rendering or controller rendering? Have you checked whether the debugger hits the view file or controller? if it is View rendering try with Html.Sitecore.ViewRendering
    – Siva Kumar
    Jul 2, 2019 at 4:36

1 Answer 1

2

The reason why Html.Sitecore.Rendering(...) is not rendered to the html of your page is because razor works in that way.

If you start a code block ({ ... }), what's inside is expected to be code, not html markup.

What you can do is:

  • Wrap what's inside if with any HTML tag, e.g.:
@if(condtion){ 
  <div>Html.Sitecore.Rendering(...)</div>
}
  • Wrap it with Razor <text> tag, which will just render the text without the tag
@if(condtion){ 
  <text>Html.Sitecore.Rendering(...)</text>
}
  • Add @:, which is equivalent with <text>:
@: @Html.Sitecore().Rendering("")

Read more in ScottGu blog post ASP.NET MVC 3: Razor’s @: and syntax

3
  • After making changes as you suggested, I can see that view being rendered on the page. But when the partial view is returing data in <script>--</script> format. It is not being rendered.
    – Shiva
    Jul 2, 2019 at 9:48
  • And for the performance, which is the effective way @Html.RenderPartial or @Html.Sitecore().Rendering(). I dont have any dependency on sitecore for data. I already have data that can be passed as Model.
    – Shiva
    Jul 2, 2019 at 9:50
  • I don't know why <script>...</script> is not rendered. This may be because of the fact that the only html tag is script. You could try wrapping script in some other html tag (unless you place it in <head> directly). If you ask about performance and you don't need any Sitecore mechanism, RenderPartial should be faster cause it doesn't need to do Sitecore extra work. But I haven't done any tests so I cannot tell you what is the difference.
    – Marek Musielak
    Jul 2, 2019 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.