开发者

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

开发者 https://www.devze.com 2023-01-12 03:16 出处:网络
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?

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?


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


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>
<% } %>


Change your view output to:

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

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

0

精彩评论

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