Html.Raw无意中拆分了字符串

时间:2018-03-16 07:30:36

标签: asp.net-mvc razor

我正在制作一个页面,客户可以在其中为自己销售的文章撰写自己的说明。他们可以使用html标签。

视图中的代码如下所示:

@Html.Raw("<p class='description short'>" + shopItem.ShortDescription + "</p>")

问题是,这被分成两个p标签:

<p class="description short"></p>

以及shopItem.ShortDescription的内容:

<p><em><strong>Title</strong></em></p>

我也试过

<p class="description short">@Html.Raw(shopItem.ShortDescription)</p>

但这会导致相同的行为。

我确定我错过了或没有看到什么。那么,该代码片段有什么问题?

1 个答案:

答案 0 :(得分:1)

我认为这里的问题是你不能嵌套p元素。如果你只用html尝试它仍然无法正常工作,但是例如这样可以正常工作

@{     
    var shopItem = "<em><strong>Title</strong></em>";
}
@Html.Raw("<p class='description short'>" + shopItem + "</p>")