I try to display a TempData
list with HTML balise inside. Is it possible to interpret the balise and not only display it ?
我尝试在内部显示一个带有HTML balise的TempData列表。是否有可能解释balise,而不仅仅是显示它?
This is code example to illustrate what I want.
这是一个说明我想要什么的代码示例。
Controller :
控制器:
List<string> ls = new List<string>();
ls.Add("<div> First div </div>");
ls.Add("<div> Another div </div>");
ls.Add("<div> Another div again </div>");
TempData.Add("ModelLs", ls);
View :
观点:
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
@str
}
And I'd like to display 3 "real" div on my page (and not 3 "<div> .... </div>"
). How can I do that ?
我想我的页面上显示3“真实”的div(而不是3”< div > ....< / div >”)。我怎么做呢?
3 个解决方案
#1
3
If i understood you right, you would like to display string as HTML markup?
如果我理解对了,您想要显示字符串作为HTML标记吗?
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
@(new HtmlString(str))
}
Html.Raw()
works too :
Html.Raw()工作:
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
@(Html.Raw(str))
}
#2
5
Use Html.Raw
it returns markup that is not HTML encoded.
使用Html。原始的it返回非HTML编码的标记。
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
Html.Raw(str)
}
#3
1
Below code worked fine for me
下面的代码对我来说很有用
@{var ls = (List<string>)TempData["ModelLs"];}
<ul>
@foreach (var str in ls)
{
if (!string.IsNullOrEmpty(str))
{
<li>
@Html.Label(str)
</li>
<br />
}
}
</ul>
#1
3
If i understood you right, you would like to display string as HTML markup?
如果我理解对了,您想要显示字符串作为HTML标记吗?
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
@(new HtmlString(str))
}
Html.Raw()
works too :
Html.Raw()工作:
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
@(Html.Raw(str))
}
#2
5
Use Html.Raw
it returns markup that is not HTML encoded.
使用Html。原始的it返回非HTML编码的标记。
var ls = (List<string>)TempData["ModelLs"];
foreach (string str in ls)
{
Html.Raw(str)
}
#3
1
Below code worked fine for me
下面的代码对我来说很有用
@{var ls = (List<string>)TempData["ModelLs"];}
<ul>
@foreach (var str in ls)
{
if (!string.IsNullOrEmpty(str))
{
<li>
@Html.Label(str)
</li>
<br />
}
}
</ul>