asp.net-mvc-3 - 如何在 MVC (Razor) 中对部分 View 进行属性编码

标签 asp.net-mvc-3 razor attributes partial-views encode

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

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

这工作得很好,但显然如果我的 PartialView 中有任何单引号,我就会遇到问题。

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

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

不幸的是,这不太有效,因为 Partial 的输出是 MvcHtmlString 而不是直字符串。

有人有更好的主意吗?

最佳答案

感谢 nemesv 的建议 - 它没有成功。经过一番思考后,我最终通过编写一个名为 PartialAttributeEncoded 的 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;
        }
    }
}

关于asp.net-mvc-3 - 如何在 MVC (Razor) 中对部分 View 进行属性编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093005/

相关文章:

asp.net-mvc-3 - ASP.NET MVC3 中的 HtmlHelper 使用 Action<T> 作为模板 : Razor syntax?

javascript - 单击按钮时将 ID 传递给操作链接

python - 列出按描述符类型过滤的对象属性

OOP:将对象与将属性传递给方法

entity-framework - 使用无参数构造函数记录?

javascript - 通过 AJAX 提交表单时保存文本框建议/自动完成

asp.net-mvc - 如何将模型传递给 View ?

asp.net-mvc-4 - 为什么 DisplayName() 和 Label() Html 助手会丢失十进制值的左侧部分?

asp.net - 如何在asp.net core 3.0中正确实现PayPal支付

python - 属性错误 : 'FreqDist' object has no attribute 'inc'