2

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?

2
  • 2
    What about @HttpUtility.HtmlAttributeEncode(Html.Partial("_MyTooltipInAPartial").ToHtmlString())?
    – nemesv
    Aug 23, 2012 at 13:48
  • Thanks - I tried this but VS complained :-( Aug 24, 2012 at 10:56

1 Answer 1

2

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;
        }
    }
}
1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.