View details ยป

How to render a link with a css class using Sitecore Glass Mapper

I have the following link:

<a class="btn btn-primary" href="#">View details ยป</a> 

How can I render a link with a glass site that it still saves the css class? With the field renderer in sitecore, you used the ability to pass the class as additional parameters, how does it work with glass?

This is what I still have:

 @RenderLink(x => x.Link) 

It only shows a link without a class.

Any help appreciated. thanks.

+8
c # hyperlink sitecore glass-mapper
source share
3 answers

You can also enable the version with the included version of PageEditor, and it should automatically take into account the Class attribute:

 @Editable(Model, x => x.Link) 

Or, when you use RenderLink, you can pass a collection with the class attribute:

 @RenderLink(x => x.Link, new System.Collections.Specialized.NameValueCollection { { "class", "btn btn-primary" } }) 

EDIT: Changed working code example and added formatting example for Editable

You can specify the format for editing:

 @(Editable<YourModelType>(Model, x => x.Link, string.Format("<a href=\"{0}\" class=\"btn btn-primary\">{1}</a>", x.Link.Url, x.Link.Text))) 
+17
source share

@Editable (x => x.Link, new {@ class = "btn btn-primary"})

+6
source share

I wrote a helper class using the Fluent API so you can easily add HTML attributes to glass helpers.

See the blog here: http://mikerobbins.co.uk/2015/07/29/sitecore-razor-glass-attribute-helper-methods-fluent-api/

You can use the helper as follows:

 @Editable(x => x.Link,new HtmlAttributes().CssClass("Link").Render()) 
+1
source share

All Articles