Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EF MVC RAZOR:如何解码PartialView输出的HTML编码字符串?_Html_Asp.net Mvc_Asp.net Mvc 3_Decode_Encode - Fatal编程技术网

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

EF MVC RAZOR:如何解码PartialView输出的HTML编码字符串?,html,asp.net-mvc,asp.net-mvc-3,decode,encode,Html,Asp.net Mvc,Asp.net Mvc 3,Decode,Encode,我正在使用EF4+MVC3和Razor 我有下面的ActionResult,它将字典呈现为局部视图 行动 public ActionResult combotest() { Dictionary<string, string> r = new Dictionary<string, string>(); r.Add("<> ''", "T"); ... return PartialView("_mypartial", r); }

我正在使用EF4+MVC3和Razor

我有下面的
ActionResult
,它将
字典
呈现为局部视图

行动

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

你能帮我吗?如果可能的话,我会避免使用
字符串。替换

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

试试这个:

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

@foreach(字典模型中的KeyValuePair值){
@价值,价值
}
返回包装原始字符串的HtmlString实例。
Razor引擎知道不转义HtmlString实例,因此显示是按预期进行的。

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

例如:

public ActionResult combotest()
{
字典r=新字典();
r、 添加(新的HtmlString(“T”);
...
返回部分视图(“我的部分”,r);
}


@foreach(字典模型中的KeyValuePair值){
@价值,价值
}


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

您能否指定其他两种方法的错误所在?非常感谢Jim!我接受詹姆斯的回答只是因为这是第一个+1泰铢
  <select>
     <option value=''></option>
       @foreach (KeyValuePair<string,string> value in (Dictionary<string, string>)Model) {
         <option value="@Html.Raw(value.Key)">@value.Value
     </option>
   }
   </select>