3

As we know, we can use this to render General Link fields in our views:

@Html.Sitecore().Field("Field Name", sourceItem)`

This will generate the usual output:

<a attribute="" another-attribute="">Link Text</a>

What if I want the rendered link to contain inner HTML such as a <span>?

4 Answers 4

3

This format can be used:

@using Sitecore.Mvc.Helpers

@{
    Sitecore.Data.Fields.LinkField linkField = sourceItem.Fields["Link1"];

    // Do some validation
}

...

@Html.Sitecore().BeginField("Field Name", sourceItem, new { haschildren = true, @class = "class-one class-two" })
    <span>@linkField.Text</span>
@Html.Sitecore().EndField()

Check out the official Sitecore documentation about how to access Link Field properties

As a side note, you should use the built in field renderers as often as possible; especially with links. A very common issue is that developers forget to add link field attributes such target="" and title="". By using the field renderer you can be certain that all of the attributes that have values will come through.

1

One very simple way would be:

@Html.Sitecore().Field(linkfieldName, new { text = "something" })

1

If you know the HTML already and also link field is specific no other changes then easy solution similar to Debu suggested -

@Html.Sitecore().Field(GLink.Fields.Link.ToString(), Model.Item,
  new
    {
       text = <span> Model.Item.LinkFieldOptions(GLink.Fields.Link, LinkFieldOption.Text) </span>
    })

Here LinkFieldOptions is an extension method which provide you more flexibility with other attributes too -

public static string LinkFieldOptions(this Item item, ID fieldId, LinkFieldOption option)
        {
            XmlField field = item?.Fields[fieldId];
            if (field == null)
            {
                return string.Empty;
            }

            switch (option)
            {
                case LinkFieldOption.Text:
                    return field.GetAttribute("text");
                case LinkFieldOption.LinkType:
                    return field.GetAttribute("linktype");
                case LinkFieldOption.Class:
                    return field.GetAttribute("class");
                case LinkFieldOption.Alt:
                    return field.GetAttribute("title");
                case LinkFieldOption.Target:
                    return field.GetAttribute("target");
                case LinkFieldOption.QueryString:
                    return field.GetAttribute("querystring");
                default:
                    throw new ArgumentOutOfRangeException(nameof(option), option, null);
            }
        }
1

Regarding the previous answer - Try to write an extension. You will end up in a position to reuse this functionality everywhere else, without writing the same stuff again and again

public static HtmlString Field(this ScHelper helper, string fieldName, Item item, object parameters, string html)
{
    if (helper == null)
    {
        throw new ArgumentNullException("helper is null");
    }
    if (html.IsNullOrEmpty())
    {
        return helper.Field(fieldName, item, parameters);
    }
    return new HtmlString(helper.BeginField(fieldName, item, parameters).ToString() + html + helper.EndField().ToString());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.