I am trying to add custom classes & inline styles to a rendered link field in the experience editor without using Glass.
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.
Edited to Add
Even using Glass as suggested below. The inline styles are removed anytime the field is edited. For example, on initial load the inline styles are there. If I make any edits such as changing the link description, the link url, or link class, then click OK to close the link dialog window. I see the changes I just made, but all the inline styles are gone. If I save the changes at this point the view will refresh and add the inline styles back.
Is there not a way to persist these styles?