我在noSelection
<g:select>
View 的Account
字段的Load
标记中使用了create
属性。当用户未进行选择并单击“创建”按钮时,我希望所有表单字段的当前值都将传递到save
Controller 中的Load
闭包中,后者依次调用create
View 并使用该 View 填充该 View 前面提到的当前值。对于我的两个表单字段,这不会发生,并且我不清楚原因。
这是Load
Controller 调用的create
/truckingmanagement/load/create
View 的样子:
如果用户未在Account
字段中进行选择,然后继续单击“创建”按钮,则显然无法创建负载。这是Load
Controller 调用的结果create
/truckingmanagement/load/save
View 的样子:
所以我在这里有两个问题,第一个问题是第二个 View 中的Logged By
字段未填充第一个 View 中选择的值。另外,下拉框完全为空,没有用户可供选择。第二个问题是Account
字段下拉框完全为空,没有可供选择的帐户。
这是<g:select>
Logged By
View 中的Account
和Load
字段使用的create
标签。Logged By
字段:
<g:select name="loggedBy.id" from="${loggedByUsers}" optionKey="id" value="${loadInstance?.loggedBy?.id}" />
Account
字段:<g:select id="account" name="account.id" from="${accountsWithCargoDestinations}" optionKey="id" noSelection="['':'-Select-']" />
create
中的save
和LoadController
闭包:def create = {
Role role1 = Role.findByAuthority("ROLE_ADMIN")
Role role2 = Role.findByAuthority("ROLE_OFFICE_PROFESSIONAL")
Role role3 = Role.findByAuthority("ROLE_DRIVER")
def loggedByUsers = UserRole.findAllByRoleInList([role1, role2, role3]).user
def loadInstance = new Load()
loadInstance.properties = params
def accountsWithCargoDestinations = Account.findAllByUserInList(Address.findAll("from Address as addresses where addresses.cargoDestination=true").user)
return [loadInstance:loadInstance, loggedByUsers:loggedByUsers, accountsWithCargoDestinations:accountsWithCargoDestinations]
}
def save = {
def loadInstance = new Load(params)
if (loadInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'load.label', default: 'Load'), loadInstance.id])}"
redirect(action: "show", id: loadInstance.id)
}
else {
System.out.println params //Trying to see if any values from the problem fields got lost.
render(view: "create", model: [loadInstance: loadInstance])
}
}
println
闭包的else
部分的save
语句中注意到,该语句打印出params
。当我这样做时,我发现在第一个 View 中的Logged By
字段中选择的值确实确实从第一个 View 一直传递到了这一点,但是我不知道为什么当Load
create
View 为在下一行代码中渲染,或者至少在渲染 View 时,为什么某些东西(即loggedByUsers
Controller 中的truckingmanagement/load/create
)没有填充Logged By
字段。任何帮助将不胜感激,并为冗长而感到抱歉。
最佳答案
回答您在评论回复中提出的问题。您可能想调查在 Controller 中使用chain的情况。如果模型中尚不存在新的loadInstance,则需要稍微修改一下create Action 。但是,这应该可以解决问题。
https://stackoverflow.com/questions/7184409/