Skip to main content
1 of 3

Sitecore MVC FieldRenderer.Render - Add inline styles & classes to links in the Experience Editor

I am trying to add custom classes & inline styles to a rendered link field in the experience editor.

I have a model the looks like this:

public class Header
{
    public HtmlString Link { get; set; }
}

My controller has a model creation method like this:

private static Header CreateModel()
{
    var item = RenderingContext.Current.Rendering.Item;
    var linkStyles = "style=Margin: 0; color: white; color: white !important; font-family: Helvetica, Arial, sans-serif; font-weight: normal; line-height: 1 !important; margin: 0; padding: 0; text-align: left; text-decoration: none;";

    var headerModel = new Header()
    {
        Link = new HtmlString(FieldRenderer.Render(item, "CTA Url",linkStyles))
    };
        
    return headerModel;
}

And in my view I am outputting this with @Model.Link

The linkStyles string in the controller works, but I cannot figure out how to add classes to this. Also, if the user adds a class in the Style class field in Sitecore, all the styles get removed & reset/replaced with the users input.