如何在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
相关产品推荐
相关产品推荐

