开发者

Using TempData to pass html? [MVC2]

I recently discovered TempData to pass messages back to the view after processing logic in the controller. But when I try and pass html with the message it is rendering the actual tags and not interpreting them as html when the page renders.

Is there any way around this or is my approached flawed?

TempData["Message"] = "<span style='color:red;'>There was a problem moving the files.</span>";

Here is what is shown in the browser:

<span style='color:red;'>There was a probl开发者_如何转开发em moving the files.</span>

Thanks!


You can decode your html in the TempData like this:

@Html.Raw(TempData["Message"].ToString())


Use <%= instead of <%: to process the string without decoding it.


I think it may be auto encoding the values; you should be able to call Server.HtmlDecode to decode it back to HTML.

HTH.


You can change style in your "View" file, like this:

@if (TempData["Message"] != null)
     {
      <span style="color:Red;">  @TempData["Message"] </span>
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜