In transitioning from Sitecore Web Forms sublayouts to Sitecore MVC renderings, there are some things that have to be done a little differently. In this post, we’ll examine the scenario of wanting to provide authors with the ability to edit text or an image inside of an editable link. Credit goes to @mhwelander who helped me out by providing the solution!

The essential issue at play here is that you no longer have the ability to ‘wrap’ server controls around each other. In Web Forms, you can place one control inside of another, but this is not supported by the Sitecore MVC markup format.

Web Forms Image inside a Link

Using the Web Forms server controls, this would be accomplished in the following way:

<sc:Link runat="server" ID="myImageLink" Field="MyLinkField">
<sc:Image Id="myImage" runat="server" Field="MyImageField" />
</sc:Link>

MVC Image passed to a link

Now, with Sitecore MVC, you need to pass the Image into the Link as a parameter:

@Html.Sitecore().Field("MyLinkField",
new {text = @Html.Sitecore().Field("MyImageField")})

Note that instead of an image control being placed inside the Link control, the image rendering output is instead passed as the text parameter for the link control.

In this way, the author will be able to edit both the link, and the image in Page Editor mode!

Got your own Sitecore MVC gotcha? Let me know!

8 Comments

  1. I am using this but it is not working
    @Html.Sitecore().Field(“Link”, new { text = Html.Sitecore().Field(“LinkText”, linkItem) })

    1. Hi there Abhay, thanks for the question! To be honest, this blog is from 2014 and I’m not sure if it still applies to today’s versions. I would recommend you checking out Sitecore.StackExchange.com, asking your question there and seeing if somebody can help you with your specific version of Sitecore (make sure to specify!)

      You can also join the community at https://sitecore.chat and talk with other developers in realtime on Slack, which would be a great way for folks to help direct you as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s