John Reilly
John Reilly

Reputation: 6269

How to Attribute encode a PartialView in MVC (Razor)

I'm using PartialViews to store the HTML for a tooltip in my ASP.NET MVC application. My initial thought was that Razor would automatically attribute encode anything sat between quotes in my HTML. Unfortunately this doesn't appear to be the case and so my workaround for now is to use single quotation marks to encapsulate my PartialViews HTML. As below:

<div class="tooltip" title='@Html.Partial("_MyTooltipInAPartial")'>Some content</div>

This works just dandy but obviously I have problems if there are any single quotation marks within my PartialView.

Does anyone know the proper approach to resolve this? The closest I've got is the below:

<div class="tooltip" title="@HttpUtility.HtmlAttributeEncode(Html.Partial("_MyTooltipInAPartial"))">Some content</div>

Unfortunately this doesn't quite work because the output of a Partial is an MvcHtmlString rather than a straight string.

Anyone got a better idea?

Upvotes: 2

Views: 1714

Answers (1)

John Reilly
John Reilly

Reputation: 6269

Thanks to nemesv for the suggestion - it didn't pan out. After a little pondering I ended up scratching my own itch by writing an HTML helper method called PartialAttributeEncoded.

For anyone interested, here's how you use the helper:

<div class="tooltip" title="@Html.PartialAttributeEncoded("_MyTooltipInAPartial")">Some content</div>

And here's the helper:

using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace My.Helpers
{
    /// <summary>
    /// MVC HtmlHelper extension methods - html element extensions
    /// </summary>
    public static class PartialExtensions
    {
        /// <summary>
        /// Allows a partial to be rendered within quotation marks.
        /// I use this with jQuery tooltips where we store the tooltip HMTL within a partial.
        /// See example usage below:
        /// <div class="tooltip" title="@Html.PartialAttributeEncoded("_MyTooltipInAPartial")">Some content</div>
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="partialViewName"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static MvcHtmlString PartialAttributeEncoded(
          this HtmlHelper helper,
          string partialViewName,
          object model = null
        )
        {
            //Create partial using the relevant overload (only implemented ones I used)
            var partialString = (model == null)
                ? helper.Partial(partialViewName)
                : helper.Partial(partialViewName, model);

            //Attribute encode the partial string - note that we have to .ToString() this to get back from an MvcHtmlString
            var partialStringAttributeEncoded = HttpUtility.HtmlAttributeEncode(partialString.ToString());

            //Turn this back into an MvcHtmlString
            var partialMvcStringAttributeEncoded = MvcHtmlString.Create(partialStringAttributeEncoded);

            return partialMvcStringAttributeEncoded;
        }
    }
}

Upvotes: 2

Related Questions