ViewData 中的 HTML 标记呈现问题?另外,MVC 中的 MultiView 功能?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
<%: %>
==Html.Encode(ViewData["Message"]);
是 HTML 编码你的字符串...试试这个:
另外,你应该能够使用
ViewData
控制部分控件,并根据ViewData
中的值,您可以渲染不同的部分。您还可以将您的视图强式输入到一个模型中,该模型代表您在页面上所追求的行为......<%: %>
==Html.Encode(ViewData["Message"]);
is HTML Encoding your string...Try this:
Also, you should be able to control Partial Controls using
ViewData
and depending on the values inViewData
you could render different partials. You could also strongly type your view to a model that represents the behavior your after on the page...您可以创建一个 MvcHtmlString
但实际上您不应该将 html 包含在你的控制器。
通过使您的视图成为强类型,您可以改为执行类似的操作。
You can create an MvcHtmlString
But really you should not have the html in your controller.
By making your view strongly-typed you could instead do something like this.
将视图输出更改为:
仅当您希望输出自动进行 HTML 编码时才使用
<%: %>
语法。Change your view output to:
Only use
<%: %>
syntax when you want output to be automatically HTML encoded.