Search code examples
c#asp.net-mvcpartial-viewsviewdata

HTML markup rendering issue in ViewData? Also, MultiView functionality in MVC?


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?


Solution

  • <%: %> == 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...