You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ServiceStack #Script的dbSelect中使用Code参数?

ServiceStack #Script模板中在dbSelect使用Code参数的正确方式

你当前写法里的{Barcode: {Code}}存在语法问题,在#Script的对象字面量中引用模板变量不需要额外加花括号,直接使用变量名即可。

修正后的完整代码

<small>Some Label</small><header>{{Code}}</header>
{{ 'select * from TrackingEntity WHERE Barcode = @Barcode' |> to => selectSql }}
{{ selectSql |> dbSelect({Barcode: Code }) |> htmlDump({ className: 'table styled-table' }) }}

说明

原代码里的{Code}会被解析成一个嵌套的空对象(而非引用模板中的Code变量),导致数据库查询时无法正确传递参数。去掉变量外的花括号后,就能正确将Code变量的值作为Barcode参数传入dbSelect执行查询。

更简洁的写法

你也可以省略中间的selectSql变量,直接内联执行:

<small>Some Label</small><header>{{Code}}</header>
{{ 'select * from TrackingEntity WHERE Barcode = @Barcode' |> dbSelect({Barcode: Code }) |> htmlDump({ className: 'table styled-table' }) }}

内容的提问来源于stack exchange,提问作者Francois Taljaard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 10:45:27