【发布时间】:2021-11-01 05:53:19
【问题描述】:
我正在使用 EF 搭建 MVC 以从 SQL Server 数据库中检索数据。我在表中有一个列有<tag> 用于超标/下标;我该如何处理?
数据库中的列值是like
Hc<1> H2<d> T18<c>
但在网络应用程序中,我需要这样显示:
所以我需要将 <sup> 和 > 替换为 </sup>
<td >
@Html.DisplayFor(modelItem => item.StrainName)
</td>
在索引页中显示该字段之前,如何转义 <tags> 并改为显示超标/下标
我试过了
@Html.DisplayFor(modelItem => item.StrainName.Replace("<i>", "").Replace("</i>", "").Replace("<sup>", "<").Replace("</sup>", ">"))
但它会抛出类似的错误
nvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Microsoft.AspNetCore.Mvc.ViewFeatures.ExpressionMetadataProvider.FromLambdaExpression<TModel, TResult>(Expression<Func<TModel, TResult>> expression, ViewDataDictionary<TModel> viewData, IModelMetadataProvider metadataProvider)
*** 编辑 ***
虽然表格没有使用任何样式
<div class="col-md-12">
<table class="table" >
<thead>
<tr>
<th width="10%">
.......
</tr>
</thead>
<tbody>
@foreach (var item in Model.Inventorys)
{
<tr style="padding-left:2em">
<td>
@Html.Raw(item.StrainName.Replace("<", "<sup>").Replace(">", "</sup>"))
</td>
.............
</tr>
}
</tbody>
</table>
【问题讨论】:
-
@Jazb 我用我尝试过的方法更新了问题
-
“我正在使用 EF 来搭建 MVC”——这是什么意思?!您的问题与实体框架有什么关系?
-
我想分享的是我没有从头开始构建视图,而是 EF 做到了
-
<1>和<d>不是 HTML 标签,这看起来像是我不熟悉的一些自定义符号,但看起来尖括号中的 everything 意味着上标?