EF MVC RAZOR:如何解码 PartialView 输出的 HTML 编码字符串?

发布于 2025-01-06 22:51:03 字数 1056 浏览 3 评论 0原文

我正在使用 EF4 + MVC 3 和 Razor。

我有以下 ActionResult,它将 Dictionary 渲染到部分视图中。

ACTION

public ActionResult combotest()
{
    Dictionary<string, string> r = new Dictionary<string, string>();
    r.Add("<> ''", "T");
    ...
    return PartialView("_mypartial", r);
}

现在,Model.Key 值中包含的特殊字符是 HTML 编码的,而我想将它们用作纯文本。例如 <> '' 呈现为 &lt;&gt; &#39;&#39;

我尝试使用 WebUtility.HtmlDecodeServer.HtmlDecode 转换它们,但没有成功:

PARTIAL VIEW (_mypartial):

<select>
    <option value=''></option>
    @foreach (KeyValuePair<string,string> value in (Dictionary<string, string>)Model) 
    {
        <option value="@WebUtility.HtmlDecode(value.Key)">@value.Value
     </option>
    }
</select>

你能帮我吗?如果可能的话,我会避免使用String.Replace

I am using EF4 + MVC 3 with Razor.

I have the following ActionResult, which renders a Dictionary<string,string> into a partial view.

ACTION

public ActionResult combotest()
{
    Dictionary<string, string> r = new Dictionary<string, string>();
    r.Add("<> ''", "T");
    ...
    return PartialView("_mypartial", r);
}

Now, special chars contained into the Model.Key values are HTML Encoded, while I'd like to use them as plain text. For example <> '' is rendered as <> ''.

I tried to convert them with WebUtility.HtmlDecode or Server.HtmlDecode without success:

PARTIAL VIEW (_mypartial):

<select>
    <option value=''></option>
    @foreach (KeyValuePair<string,string> value in (Dictionary<string, string>)Model) 
    {
        <option value="@WebUtility.HtmlDecode(value.Key)">@value.Value
     </option>
    }
</select>

Could you help me? I would avoid to use String.Replace, if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2025-01-13 22:51:03

要显示未编码的文本,您可以使用 @Html.Raw(value.key)

To display text unencoded you can use @Html.Raw(value.key)

趁年轻赶紧闹 2025-01-13 22:51:03

Larry,

试试这个:

  <select>
     <option value=''></option>
       @foreach (KeyValuePair<string,string> value in (Dictionary<string, string>)Model) {
         <option value="@Html.Raw(value.Key)">@value.Value
     </option>
   }
   </select>

Html.Raw() 返回一个包装原始字符串的 HtmlString 实例。
Razor 引擎知道不会转义 HtmlString 实例,因此显示按预期进行。

Larry,

try this:

  <select>
     <option value=''></option>
       @foreach (KeyValuePair<string,string> value in (Dictionary<string, string>)Model) {
         <option value="@Html.Raw(value.Key)">@value.Value
     </option>
   }
   </select>

Html.Raw() returns an HtmlString instance that wraps the original string.
The Razor engine knows not to escape HtmlString instances, thus display is as intended.

南风几经秋 2025-01-13 22:51:03

首先考虑不要使用“字符串”,而是使用 IHtmlString,例如使用 HtmlString

例如:

public ActionResultcombotest()
{
字典r = new Dictionary();
r.Add(new HtmlString("<> ''"), "T");
...
返回 PartialView("_mypartial", r);
}

<选择>;
<选项值=''>
@foreach (KeyValuePairvalue in (Dictionary)Model) {
<选项值=“@value.Key”>@value.Value

}

现在,您不必依赖视图(使用 Html.Raw)和控制器(提供希望安全的文本)之间的隐式安全契约。您提供有效、安全的 Html,并从源头对其进行标记。

Consider not using 'string' in the first place, but rather IHtmlString, for example with HtmlString.

For example:

public ActionResult combotest()
{
Dictionary<IHtmlString, string> r = new Dictionary<IHtmlString, string>();
r.Add(new HtmlString("<> ''"), "T");
...
return PartialView("_mypartial", r);
}

<select>
<option value=''></option>
@foreach (KeyValuePair<IHtmlString,string> value in (Dictionary<IHtmlString, string>)Model) {
<option value="@value.Key">@value.Value
</option>
}
</select>

Now, you don't have to rely on an implicit security contract between the view (using Html.Raw) and the controller (providing text that is hopefully safe). You're providing valid, safe Html and marking it as such from the source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文