enter image description herethis is the value on my Rich text field:

<p><strong>Did you know the monarch population has declined by more than 90 percent?</strong> Did you know that much of the monarch summer breeding habitats have been destroyed due to agricultural and gardening practices in the United States? </p>
<p>As part of National Wildlife Federation's&nbsp;<strong>Garden for Wildlife</strong> program, Butterfly Heroes seeks to bring awareness to the declining population and connect gardeners and kids and families alike to help the monarchs and other pollinators. Take the pledge to help to create new habitats for monarch butterflies by providing a place to rest their wings and lay their eggs.&nbsp;<a href="http://www.nwf.org/Butterfly-Heroes/Pledge.aspx">Submit your photo pledge and become a Butterfly Hero &gt;&gt;</a></p>

this is my c# code:

    var sectionName = Sitecore.Context.Item.Fields["Name"];
    var pageTitle = Sitecore.Context.Item.Fields["Title"];
    var mainContent = Sitecore.Context.Item.Fields["Main Content"].Value;
    Sitecore.Data.Fields.ImageField linkIcon = (Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["Image"];
    var linkIconUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(linkIcon.MediaItem);

   <div class='col-xs-12 col-md-9 content'>

                    <div class="pull-right"><img width="219" height="219" class="img-responsive" alt="OB9" src="@linkIconUrl" /></div>


Its because you are not using the Sitecore field render to render the field content. This code:

var mainContent = Sitecore.Context.Item.Fields["Main Content"].Value;

just outputs the raw value of the field. It bypasses all of the Sitecore field rendering pipeline. This includes all the markup, and because razor automatically encodes html in variables you are seeing that encoding output.

Also - if the field is null, this code would throw a null reference exception.

The correct way to render a field if you want to use the raw Sitecore API and not use an ORM/Wrapper would be to use the Sitecore Html helper class and call the field renderer like this:

var mainContent = Html.Sitecore().Field("Main Content");

In fact you don't really need to put the value in a variable. This would work:

<div class='col-xs-12 col-md-9 content'>
        <div class="pull-right">
            @Html.Sitecore().Field("Image", new { @class = "img-responsive" })
        @Html.Sitecore().Field("Main Content")

Doing it this way means that you will get all the experience editor functions also.

If you want to really look at best practices - you should be using an ORM or Wrapper such as Glass Mapper or Fortis and using strongly typed models to render your Sitecore content. Using field name strings like this will result in un-maintainable code.

| improve this answer | |
  • thank you ... we may use glass mapper for this project, but we are not using it right now. I changed the code to @html.Sitecore().Field and that took care of this. – Paul T. Rykiel Jun 27 '17 at 21:11

When you use @ to render a variable that includes HTML, it will automatically escape it. If you want to render the variable as HTML, use @Html.Raw()

That said, I would recommend you look at using the Sitecore MVC helpers to render fields instead of manually extracting fields and values. A quick Google turned up the following: http://sitecore-community.github.io/docs/sitecore-mvc/rendering-content/ Basically, the gist of it is to use @Html.Sitecore().Field("field name") when rendering fields.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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