views:

53

answers:

3

Hi, i'm trying to populate a ViewData instance with html markup like shown below. When the page renders, the html tags get rendered as text and not html markup. Anyone know why?

Controller code:

if (user.ActivationStatus == false)
{
    ...
    ViewData["Message"] = "<p>Your account has been activated, you're good to go.</p>";
}
    else 
{
    ViewData["Message"] = "<p>Sorry, this account has already been activated.</p>";
}
return View(); 

View code:

<h2>Confirmation</h2>
<p><%: ViewData["Message"] %></p>

Secondly, I have used the MultiView feature in asp.net webforms in the past. This functionality is ideal and like to implement a similar functionality in MVC.

Is there any way i can call different PartialViews dependant on the function outcome(like the 'if' statement above) from controller code to a placeholder in the View?

A: 

<%: %> == Html.Encode(ViewData["Message"]);is HTML Encoding your string...

Try this:

<%= ViewData["Message"]%>

Also, you should be able to control Partial Controls using ViewData and depending on the values in ViewData you could render different partials. You could also strongly type your view to a model that represents the behavior your after on the page...

Alexander
A: 

Change your view output to:

<h2>Confirmation</h2>
<p><%= ViewData["Message"] %></p>

Only use <%: %> syntax when you want output to be automatically HTML encoded.

Dan Diplo
+1  A: 

You can create an MvcHtmlString

<%: MvcHtmlString.Create(ViewData["Message"]) %>

But really you should not have the html in your controller. By making your view strongly-typed you could instead do something like this.

<h2>Confirmation</h2>
<% if(Model.ActivationStatus) { %>
  <p>Sorry, this account has already been activated.</p>
<% } else { %>
  <p>Your account has been activated, you're good to go.</p>
<% } %>
Jesper Palm