Reputation: 61
Based on the question/answer here, I can successfully render a field with a class attribute.
For example, I have a list of link fields that are being rendered with the Bootstrap class nav-link
. However, if any of the links have Style class:
defined in Sitecore, setting the nav-link
class will overwrite the style class, rather than add to it.
How can I add the nav-link class without overwriting any classes added through Sitecore?
Example code:
@Html.Sitecore().Field("Link", navItem, new { @class = "nav-link" })
Upvotes: 3
Views: 1141
Reputation: 61
I discovered the answer by casting the field as a LinkField
as follows:
@Html.Sitecore().Field("Link", navItem, new {
@class = "nav-link " + ((LinkField)navItem.Fields["Link"]).Class,
text = Html.Sitecore().Field("Link Text", navItem)
})
As can be seen in the code above, we simply concatenate the desired class with ((LinkField)navItem.Fields["Link"]).Class
, and that gives us both classes on the field.
Upvotes: 3