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?
@HttpUtility.HtmlAttributeEncode(Html.Partial("_MyTooltipInAPartial").ToHtmlString())
?