How to render link with css class with Sitecore Glass Mapper

送分小仙女□ 提交于 2019-12-08 16:07:29

问题


I have the following link:

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

How can I render the link with sitecore glass that it still keeps the css class? With the field renderer in sitecore you used to be able to pass the class along as additional parameters, how does this work with glass?

This is what I have so far:

@RenderLink(x => x.Link)

This only renders the link without the class though.

Any help appreciated. Thx.


回答1:


You can also make a PageEditor enabled version like this and it should automatically take the Class attribute into account:

@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: Modified example to working code and added formatting example for Editable

You can specify a format for Editable:

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



回答2:


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




回答3:


I have written a helper class using a Fluent API to allow you to add HTML attributes to glass helpers easily.

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

You can use the helper like this:

@Editable(x => x.Link,new HtmlAttributes().CssClass("Link").Render())


来源:https://stackoverflow.com/questions/19587981/how-to-render-link-with-css-class-with-sitecore-glass-mapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!