通过MvcHtmlString.Create()渲染撇号 [英] Render apostrophe through MvcHtmlString.Create()

查看:121
本文介绍了通过MvcHtmlString.Create()渲染撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如下所示: Foo& s Bazz



我正在使用定制HtmlHelper在div元素内呈现此文本:

  public static MvcHtmlString DisplayWithReadonlyIdFor< TModel,TValue>(this HtmlHelper< TModel> ;表达式< Func< TModel,TValue>>表达式,对象htmlAttributes)
{
string readonlyId =Readonly+ helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression) );

TagBuilder tag = new TagBuilder(div);
tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));

tag.SetInnerText(helper.DisplayFor(expression).ToString());

return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}

使用此代码,我仍然可以看到撇号的HTML表示,而不是撇号本身。

我试着按照通过调用JavaScriptStringEncode转义来自MVC 3 Razor View变量的单引号。我也试过HtmlDecode。



我想知道我需要做些什么才能实现实际的撇号,而不是我div元素中的HTML标记。

解决方案



  HttpUtility.HtmlDecode(Some value); 
HttpUtility.HtmlEncode(Some value);

示例

  string value1 =& lt; html& gt;; //&安培; LT; HTML&安培; GT; 
string value2 = HttpUtility.HtmlDecode(value1); //< HTML>
string value3 = HttpUtility.HtmlEncode(value2); //&安培; LT; HTML&安培; GT;

MvcHtmlString.Create(HttpUtility.HtmlDecode(Some value))


I have a string which looks like: Foo&#39;s Bazz

I am using a custom HtmlHelper to render this text inside of a div element:

public static MvcHtmlString DisplayWithReadonlyIdFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
    string readonlyId = "Readonly" + helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));

    TagBuilder tag = new TagBuilder("div");
    tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));

    tag.SetInnerText(helper.DisplayFor(expression).ToString());

    return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}

Using this code, I still see the HTML representation of apostrophe, not the apostrophe itself.

I tried following the advice found in Escaping single quote from an MVC 3 Razor View variable by calling JavaScriptStringEncode. I've also trying HtmlDecode. Neither have any effect.

I was wondering what I need to do to achieve rendering the actual apostrophe instead of just the HTML mark-up inside of my div element.

解决方案

Did you try below ?

HttpUtility.HtmlDecode("Some value");
HttpUtility.HtmlEncode("Some value");

Example

string value1 = "&lt;html&gt;";                 //&lt;html&gt;
string value2 = HttpUtility.HtmlDecode(value1); //<html>
string value3 = HttpUtility.HtmlEncode(value2); //&lt;html&gt;

MvcHtmlString.Create(HttpUtility.HtmlDecode("Some value"))

这篇关于通过MvcHtmlString.Create()渲染撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆