EF MVC RAZOR:如何解码 PartialView 输出的 HTML 编码字符串?
我正在使用 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 编码的,而我想将它们用作纯文本。例如 <> ''
呈现为 <> ''
。
我尝试使用 WebUtility.HtmlDecode
或 Server.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要显示未编码的文本,您可以使用
@Html.Raw(value.key)
To display text unencoded you can use
@Html.Raw(value.key)
Larry,
试试这个:
Html.Raw() 返回一个包装原始字符串的 HtmlString 实例。
Razor 引擎知道不会转义 HtmlString 实例,因此显示按预期进行。
Larry,
try this:
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.
首先考虑不要使用“字符串”,而是使用
IHtmlString
,例如使用HtmlString
。例如:
public ActionResultcombotest()r = new Dictionary();
{
字典
r.Add(new HtmlString("<> ''"), "T");
...
返回 PartialView("_mypartial", r);
}
<选择>;value in (Dictionary)Model) {
<选项值=''>
@foreach (KeyValuePair
<选项值=“@value.Key”>@value.Value
}
现在,您不必依赖视图(使用 Html.Raw)和控制器(提供希望安全的文本)之间的隐式安全契约。您提供有效、安全的 Html,并从源头对其进行标记。
Consider not using 'string' in the first place, but rather
IHtmlString
, for example withHtmlString
.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.