asp.net-mvc - MVC:从模型/控制器传递到视图时如何将字符串解析为 html dom 元素
问题描述
其中一个模型具有“描述”属性,该属性以字符串格式返回 html。喜欢 ”... ”。在没有 Js 代码的情况下加载页面时如何在视图中显示它。直接在这一行:
<td style="max-width: 600px">
<%: Html.DisplayFor(modelItem => item.description) %>
</td>
我应该放什么而不是“ modelItem.description ”?
我试过了
<%: Html.DisplayFor(modelitem =>HttpUtility.HtmlEncode(item.description)) %>
但它返回以下错误
System.InvalidOperationException: 'Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.'
解决方案
原因是因为 我不能在Displayfor<%: Html.DisplayFor... %>
中使用编码
以下是显示它的正确方法之一:
<%: Html.Raw(HttpUtility.HtmlDecode(item.description))%>
推荐阅读
- python - 使用 Python ElementTree 在 XML 中查找兄弟元素文本值
- android - Flutter android风味生成apk
- python - Pyqt中的永久后台QThread向Main类发出信号
- python - 是否可以通过单个变量将未知数量的变量传递到使用 args 和 kwargs 的函数调用中?
- php - 如何在表单构建器中对 entityType 字段的 group_by 选项进行排序?
- java - 为什么 forEach(System.out::println) 有效但 map(System.out::println) 失败?
- lambda - 通过维护输入列表中元素的顺序将多个列表合并为单个列表
- docker - 如何在 docker-compose.yml 中指定构建时 ENV
- java - 有没有办法在第一个内部的另一个方法调用完成之前留下一个方法?
- javascript - “未定义火力基地”有什么问题?