Sitecore glass mapper shared link in mvc 4

Hi, I'm learning sitecore 7 with MVC4 and glassmapper right now, and I'm having some problems with the shared link field. It seems that I cannot correctly derive external links (and not links to elements) from the general link field. What am I doing wrong?

My model:

[SitecoreType(TemplateId = "{F8168BAF-6916-47FE-BC7F-DE3B033CE233}")] public class SocialLink : AbstractBase { [SitecoreField] public virtual string Description { get; set; } [SitecoreField] public virtual string Class { get; set; } [SitecoreField(FieldType = SitecoreFieldType.GeneralLink)] public virtual Link Url { get; set; } } 

in view:

 @foreach (var socialLink in Model.SocialFolder.Socials) { <a href="@socialLink.Url" class="connect @socialLink.Class">@socialLink.Description</a> } 

Output:

 <a href="Glass.Mapper.Sc.Fields.Link" class="connect slideshare">Read us on Slideshare</a> 

Thanks in advance.

+6
source share
2 answers

Is the model auto-generated or have you created it manually? What type of Link is Glass.Mapper.Sc.Fields.Link ? If you need @socialLink.Url.Url , you want the Url property from the Link field to be called Url.

 @foreach (var socialLink in Model.SocialFolder.Socials) { <a href="@socialLink.Url.Url" class="connect @socialLink.Class">@socialLink.Description</a> } 

It would be very difficult for me to rename the Class and Url properties to something else, perhaps CssClass and SocialMediaUrl or something, so as not to cause confusion.

+7
source

To support bindings and time periods, it is better to use link.BuildUrl((SafeDictionary<string>)null)

There are two methods Link.BuildUrl() , and, paradoxically, both parameters have default parameters (although one of them is marked as deprecated). You will need to indicate which one is entered using the null character or ...

You can add an extension method that makes it easier

 public static class GlassFieldExtensions { public static string GetUrl(this Link link) { return link.BuildUrl(null as SafeDictionary<string>); } } 

And in HTML:

 @foreach (var socialLink in Model.SocialFolder.Socials) { <a href="@socialLink.GetUrl()" class="connect @socialLink.Class">@socialLink.Description</a> } 
0
source

All Articles