ViewData 中的 HTML 标记呈现问题?另外,MVC 中的 MultiView 功能?

发布于 2024-09-14 06:21:48 字数 668 浏览 3 评论 0原文

我正在尝试使用 html 标记填充 ViewData 实例,如下所示。当页面呈现时,html 标记将呈现为文本而不是 html 标记。有人知道为什么吗?

控制器代码:

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(); 

视图代码:

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

其次,我过去曾在asp.net webforms中使用过MultiView功能。此功能非常理想,就像在 MVC 中实现类似的功能一样。

有什么方法可以根据函数结果(如上面的“if”语句)从控制器代码调用不同的 PartialViews 到视图中的占位符?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤寂小茶 2024-09-21 06:21:48

<%: %> == Html.Encode(ViewData["Message"]);是 HTML 编码你的字符串...

试试这个:

<%= ViewData["Message"]%>

另外,你应该能够使用 ViewData 控制部分控件,并根据 ViewData 中的值,您可以渲染不同的部分。您还可以将您的视图强式输入到一个模型中,该模型代表您在页面上所追求的行为......

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

花海 2024-09-21 06:21:48

您可以创建一个 MvcHtmlString

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

但实际上您不应该将 html 包含在你的控制器。
通过使您的视图成为强类型,您可以改为执行类似的操作。

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

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>
<% } %>
隔岸观火 2024-09-21 06:21:48

将视图输出更改为:

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

仅当您希望输出自动进行 HTML 编码时才使用 <%: %> 语法。

Change your view output to:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文