Asp.net mvc 3 如何在MVC(Razor)中对局部视图进行属性编码

Asp.net mvc 3 如何在MVC(Razor)中对局部视图进行属性编码,asp.net-mvc-3,razor,attributes,partial-views,encode,Asp.net Mvc 3,Razor,Attributes,Partial Views,Encode,我正在使用PartialView在ASP.NET MVC应用程序中存储工具提示的HTML。我最初的想法是Razor会自动对HTML中引号之间的任何内容进行属性编码。不幸的是,情况似乎并非如此,因此我目前的解决方法是使用单引号来封装我的PartialViewsHTML。详情如下: <div class="tooltip" title='@Html.Partial("_MyTooltipInAPartial")'>Some content</div> 一些内容 这很管用,

我正在使用PartialView在ASP.NET MVC应用程序中存储工具提示的HTML。我最初的想法是Razor会自动对HTML中引号之间的任何内容进行属性编码。不幸的是,情况似乎并非如此,因此我目前的解决方法是使用单引号来封装我的PartialViewsHTML。详情如下:

<div class="tooltip" title='@Html.Partial("_MyTooltipInAPartial")'>Some content</div>
一些内容
这很管用,但如果在我的PartialView中有任何单引号,显然我会遇到问题

有人知道解决这个问题的正确方法吗?我得到的最接近的数据如下:

<div class="tooltip" title="@HttpUtility.HtmlAttributeEncode(Html.Partial("_MyTooltipInAPartial"))">Some content</div>
一些内容
不幸的是,这并不能很好地工作,因为分部的输出是一个MvcHtmlString而不是一个直字符串


有人有更好的主意吗?

多亏了涅梅斯夫的建议——结果没有成功。经过一番思考后,我最终写了一个名为
partialAttributencoded
的HTML助手方法,这让我感到很不舒服

对于任何感兴趣的人,以下是如何使用帮助器:

<div class="tooltip" title="@Html.PartialAttributeEncoded("_MyTooltipInAPartial")">Some content</div>
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;
        }
    }
}
一些内容
下面是助手:

<div class="tooltip" title="@Html.PartialAttributeEncoded("_MyTooltipInAPartial")">Some content</div>
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;
        }
    }
}
使用System.Web;
使用System.Web.Mvc;
使用System.Web.Mvc.Html;
名称空间My.Helpers
{
/// 
///MVC HtmlHelper扩展方法-html元素扩展
/// 
公共静态类particle
{
/// 
///允许在引号内渲染局部。
///我将其与jQuery工具提示一起使用,在jQuery工具提示中,我们将工具提示HMTL存储在一个部分中。
///请参见下面的示例用法:
///一些内容
/// 
/// 
/// 
/// 
/// 
公共静态MvcHtmlString PARTIALATTRIBUTENCODED(
这个HtmlHelper助手,
字符串partialViewName,
对象模型=空
)
{
//使用相关重载创建分部(仅使用实现的重载)
var partialString=(model==null)
?helper.Partial(partialViewName)
:helper.Partial(partialViewName,model);
//属性对部分字符串进行编码-请注意,我们必须.ToString()这样才能从MvcHtmlString返回
var partialstringattributencode=HttpUtility.htmlattributencode(partialString.ToString());
//将其转换回MvcHtmlString
var partialMvcStringAttributeEncoded=MvcHtmlString.Create(partialStringAttributeEncoded);
返回partialMvcStringAttributeEncoded;
}
}
}

@HttpUtility.htmlattributencode(Html.Partial(“\u MyTooltipInAPartial”).ToHtmlString())怎么样?
?谢谢-我试过了,但VS抱怨:-(我最后把它变成了一篇博客文章,你可以在这里找到: