Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF MVC RAZOR: How to decode HTML Encoded strings of a PartialView output?

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 &lt;&gt; &#39;&#39;.

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.

like image 491
Larry Avatar asked Feb 16 '12 12:02

Larry


1 Answers

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

like image 81
James Santiago Avatar answered Nov 13 '22 20:11

James Santiago