7

Hi i'm currently learning sitecore 7 with MVC4 and glassmapper and I'm having some issues with the general linkfield. I can't seem to ouput the external links (not links to items) correctly from a general linkfield. 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 the 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.

Filip Huysmans
  • 1,301
  • 1
  • 20
  • 44

2 Answers2

7

Is the model auto-generated or did you create them manually? What type is Link, Glass.Mapper.Sc.Fields.Link? If so you need @socialLink.Url.Url, you want the Url property from the Link field called Url.

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

I would be very tempted to rename Class and Url properties to something else, possibly CssClass and SocialMediaUrl or something so as not to cause confusion.

jammykam
  • 16,940
  • 2
  • 36
  • 71
  • The model is manually created. With the '.url' behind it is working. thanks for that and I will change the names to stop confusion. To further enhance this, do you know what the best is way to create an editable link field for the page editor? – Filip Huysmans Jan 06 '14 at 15:34
  • @FilipHuysmans You should just use a single LinkField, there is no need for `Description` or `Class` since you can set those can be [set in the properties of the link](http://i.imgur.com/vSyCkLX.png). Then you can make the field [Editable in Glass](http://glass.lu/docs/tutorial/sitecore/tutorial05/tutorial05.html) – jammykam Jan 06 '14 at 16:48
  • I've tried something else: @RenderLink(socialLink, x=>x.Url, new NameValueCollection {{"class", "connect " + socialLink.Class}}, true, contents: socialLink.Description) But when isEditable is set to true, it is not showing up in the page editor. The field is just invisible but on normal mode everything works. When I delete the contents parameter it works in the page editor. Any thoughts on this? – Filip Huysmans Jan 07 '14 at 09:20
  • @FilipHuysmans Try http://stackoverflow.com/questions/19587981/how-to-render-link-with-css-class-with-sitecore-glass-mapper – jammykam Jan 07 '14 at 14:27
  • Do you know how I can use a different field as the description of the link. I can only use the description from the link field with: @(Editable(socialLink, x => x.Url,new {@class=String.Format("connect {0}", socialLink.Class)})) and I can't use the page editor with: @(RenderLink(socialLink, x=>x.Url, new NameValueCollection {{"class", "connect " + socialLink.Class}},true, socialLink.Description)) – Filip Huysmans Jan 07 '14 at 15:25
  • `@(Editable(Model, x => x.Link, string.Format("{1}", x.Link.Url, x.Link.Text)))` from the link I gave you didn't work? Otherwise no, I don't know any other way than using just the Link field as I suggested - which makes sense this is control editor that Sitecore provides, how do you tell it which parts of the link should be saved to which field? – jammykam Jan 07 '14 at 16:13
  • It works if I just use the general link field without any additional fields. With the sc:Link field I was able to combine a link field with other fields and make everything editable but this seems not to be possible in mvc with glass mapper. Thanks for your advice on this matter! – Filip Huysmans Jan 08 '14 at 11:17
  • [Mike Edwards](https://twitter.com/mikeedwards83) is probably best able to answer, I'm not sure myself. You could try messaging him. – jammykam Jan 08 '14 at 14:03
1

So that Anchors and Querystrings are supported, it's best to use link.BuildUrl((SafeDictionary<string>)null)

There are two Link.BuildUrl()methods and, annoyingly, they both have default parameters (although one marked as obsolete). You will need to specify which one via a typed null, or...

You can add an extension method which will make things easier

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

And in the HTML:

@foreach (var socialLink in Model.SocialFolder.Socials)
{
     <a href="@socialLink.GetUrl()" class="connect @socialLink.Class">@socialLink.Description</a>                
}
Dan
  • 12,808
  • 7
  • 45
  • 54