grails - 当用户将表单字段留空时,使用正确的参数重新渲染 View 时出现问题

我在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 中的AccountLoad字段使用的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中的saveLoadController闭包:
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/

相关文章:

grails - 在Grails的Jasper中将集合用作参数

grails - 为什么Save无法在Grails中使用模拟域?

grails - 使用类为M:M关系建模时,Grails 2.0在Bootstrap中服务于不同的行

grails - 将Grails 1.3.7与EclipseLink 2.2.0 JPA Provi

data-binding - Grails 绑定(bind)集合枚举

grails - Spring Security UI,有关其他补充表单/UI插件的推荐

grails - 同一页上有多个表和可排序列的问题

hibernate - Grails GORM映射尝试访问不存在的字段 “class”

grails - 在对象已经与该类持久化之后修改该类

grails - 在选项卡中,我有两个域