razor - 如何在 MVC Razor 中动态设置 <% = %>
问题描述
我需要在我的剃须刀代码中动态添加一个关键字段名称。
我只是不确定如何正确设置它,所以 data.KeyNameField 是在运行时设置的
我在剃须刀中尝试了 string.format,但我认为它不会起作用,因为 <% %> 对于在客户端上设置键字段很重要
.CellTemplate(
@<text>
<a href="@Html.Raw(url)?id=<%= data.THE_GUID%>">@Model.OpenRecord.OpenRecordButtonText</a>
</text>);
}
为了能够动态地传递我的关键字段名称,我可以将唯一 ID 添加到我的 URL 中。
我需要用我的键字段名称模型上的属性替换 .'THE_GUID'
解决方案
回答:
if (Model.HasOpenRecordButton && Model.OpenRecord != null)
{
string urlAction = Url.Action(Model.OpenRecord.OpenRecordButtonAction, Model.OpenRecord.OpenRecordButtonController);
string rawUrl = string.Format("{0}?id=<%= data.{1} %>", urlAction, Model.KeyFieldName);
column
.Add()
.VisibleIndex(Model.OpenRecord.ColumnPositionIndex)
.Caption(Model.OpenRecord.Caption)
.DataType(GridColumnDataType.String)
.Width(Model.OpenRecord.ColumnWidth)
.CellTemplate(
@<text>
<a href="@Html.Raw(rawUrl)">@Model.OpenRecord.OpenRecordButtonText</a>
</text>
);
}
其中 data.{1} 将是关键字段名称,并将将该字段的值绑定为 id=*
推荐阅读
- python - 用 joblib Parallel 填充数组
- javascript - 使用javascript动态单击按钮时如何在innerHTML中显示数据?
- react-native - Formic 组件导致“错误:元素类型无效”。无法确定原因
- javascript - Nodejs Sequelize 的 FindOne() 模型包含舍入或截断的值
- vue.js - Vuejs - 如何添加多个加载指示器(微调器)以彼此具有不同的状态?
- html - 为什么溢出会覆盖父容器宽度?
- node.js - Angular SSR - 通用,
- kubernetes - 在 traefik TCP 路由映射中使用 hostSNI(*) 有什么缺点
- xslt - p & imag 移入 observ 元素 - XSLT
- html - 如何使用 display flex 在图像网格下方设置