0

I'm using SXA 1.8 with Sitecore 9.1

For a cloned rendering - in my cshtml I need to conditionally wrap a list item in a link. I have got the link part working, but I can't seem to add title and aria-label attributes?

This is what I've tried in my code:

@Html.Sitecore().BeginField("Link", item, new { haschildren=true, title=item.Fields["DescriptionLinkTitle"], @aria_label = item.Fields["DescriptionLinkTitle"] })

I'm trying not to resort to having something like

<a href="@url" title="@titlefield" aria-label="@titlefield">
  stuff
</a>

have also tried @title.

Note: The haschildren is just because I don't want the text of the link displayed.

The source output is just

<a href="/">
  stuff
</a>

So it seems to be ignoring the other attributes I'm trying to add.

  • Can you try to add .Value at the end of the fields? Example: item.Fields[“fieldname”].Value. Also, make sure the fields contains data and is published to Web – Hishaam Namooya Mar 20 '19 at 20:41
  • @HishaamNamooya that was it thanks. I had actually tried .Value and was throwing an exception. I swear I published everything, but today it published some new things and I added logic to check for null :) Feel free to post as an answer so I can accept it. – Jen Mar 20 '19 at 23:16
  • 3
    It would be better to change it to item["DescriptionLinkTitle"], .Value will throw an exception if the field is empty (you'll need to add a null check) whereas this will just return an empty string – jammykam Mar 21 '19 at 2:50
  • @jammykam good to know thanks! turns out I needed to publish AND spell my field correctly :D – Jen Mar 21 '19 at 4:58
  • @Jen comment converted to answer – Hishaam Namooya Mar 21 '19 at 5:53
2

You can use both

item.Fields[“fieldName”].Value

Or as Jammykam mentioned

item[“fieldName”]

However, when using the .Value, you need to check for null and it will cause Null Exception if blank.

Finally, make sure that

  1. The FieldNameis properly set, i.e same as in Sitecore Template.

  2. The item has been published to the web database

| improve this answer | |
  • Your mention on mine is still wrong, it should be item["fieldname"] – jammykam Mar 21 '19 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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