如何在ASP.NET MVC中渲染HTML字符串?

33 浏览
0 Comments

如何在ASP.NET MVC中渲染HTML字符串?

在我的主页上,我有代码@{Html.RenderPartial(\"_Partial1.cshtml\");},而在我的Partial中,我有一个HTML字符串:

@{ 
    // The string is actually dynamic, not static. This is here for simplicity
    string abc="abc";
} 
@abc

。我想使用一些CSS错误样式输出abc,但我实际上得到了abc——当然,没有样式。我该如何使它被解释为HTML源代码而不是字符串?

admin 更改状态以发布 2023年5月24日
0
0 Comments

如果您在视图中使用模型,请使用以下方法:

@model YourApp.Models.YourModel
....
@Html.Raw(@Model.NameOfYourProperty)

0
0 Comments

可以使用 Html.Raw() 方法来实现。

0