我的视图中有一个dropdown (g:select)
,它显示来自域类的列表。我想在点击保存按钮时打印其中的任何值。下面是视图
<g:select params="[tablename: storehouseInstance?.tablename]" name="tablename" from="${Warehouse.list()}" optionKey="id" optionValue="${Warehouse.list()}" value="tablename"
noSelection="['':'Choose Table']"
onchange="${remoteFunction (
controller: 'warehouse',
action: 'updateSelect',
params: "'warehouse.id=' + this.value + storehouseInstance?.tablename" ,
param: "this.value",
update: [success:'fieldSelection'] //update: 'fieldselection'
)}"/>
在我的控制器中我有:
def save() {
println("params are "+ params)
println("Saving table name: " + params.tablename)
}
我得到tablename是'1',而不是应该是'MYTESTVALUE'的值
请帮忙
假设仓库的name属性为name
,则尝试以下操作:
<g:select params="[tablename: storehouseInstance?.tablename]"
name="tablename"
from="${Warehouse.list()}"
optionKey="name" // I changed this line
optionValue="name" // I changed this line too
value="tablename"
noSelection="['':'Choose Table']"
onchange="${remoteFunction (
controller: 'warehouse',
action: 'updateSelect',
params: "'warehouse.id=' + this.value + storehouseInstance?.tablename" ,
param: "this.value",
update: [success:'fieldSelection']
)}"/>
在g:select
中,发送给服务器的参数由optionKey
定义,下拉框中显示的值由optionValue
定义
从MVC纯度的角度来看,你不应该在gsp中做查询,例如Warehouse.list()
。相反,您应该检索服务/控制器中所需的数据,并通过模型将其传递给视图(GSP)。