1

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 problem moving the files.</span>

Thanks!

2
  • You're only showing one side of the problem. The real enlightening part is the part that extracts tempdata and displays it. Can you show that code? Commented Dec 30, 2010 at 19:59
  • Nope, TempData is what I'm using. On the View side I was simply doing: <%: TempData["Message"]%>, but Keith had me change it to <%= TempData["Message"]%> and all is good! Commented Dec 30, 2010 at 21:18

4 Answers 4

12

You can decode your html in the TempData like this:

@Html.Raw(TempData["Message"].ToString())
Sign up to request clarification or add additional context in comments.

2 Comments

Alert: null reference exception
check for null first. like answer below
1

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

Comments

0

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

HTH.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.