Grails - 如何将一个 ID 从 View 发送到 Controller (多对多关系)

标签 grails many-to-many parameter-passing grails-2.4

我正在尝试发送迭代项目的 ID,但我认为正在发送整个列表。如何只发送一个 ID?
我有 STUDENTCOURSE域类。

领域模型

class Student {
    String fullName
    String toString() {
        "$fullName"
    }

    static belongsTo = [school: School]
    static hasMany = [courses:Course, studentCourses:StudentCourse]
}

class Course {
    String course
    String toString() {
        "$course"
    }

    static hasMany = [studentCourses:StudentCourse]
    static belongsTo = Student

}

class StudentCourse {
    Student student
    Course course

    //Some methods...
}

这是我的编辑 View 。
<g:if test="${studentInstance.studentCourses?.course}">
        <g:each class="courseList" in="${studentInstance.studentCourses?.course}" var="courses">
            <li class="courseList">
                <span class="courseList" aria-labelledby="courses-label">${courses.id} ${courses}
                <g:actionSubmitImage class="deleteIcon" action="deleteCourse" value="delete"
                 src="${resource(dir: 'images', file: 'delete.png')}" height="17"
                 onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');"
                 params="${courses.id}"/></span>
            </li>
        </g:each>
    </g:if>

当用户单击 delete.png 时,我希望能够从列表中删除一门类(class)图片。但是当我println params.course参数作为整个列表发送,而不是作为列表的单个项目发送,即使它在 g:each 内标签。我怎样才能只向 Controller 发送一个相应的项目?

我的编辑页面有一个类(class)列表。
Course 23  English (delete icon here)
       42  Science (delete icon here)
       67  Math (delete icon here)

在我的 println params.course这就是我所看到的。
[ English, Science, Math ]

我怎么能有[English]当用户单击 English 旁边的删除按钮时线?

先感谢您。

最佳答案

据我了解您的问题,问题应该在您的标签库中

我给你一个类似代码的例子,这是有效的。

<g:each in="${student?.studentCourses?.course}" var="course">
<div>
    <span>${course.id} ${course.course}</span>
    <g:link controller="login" action="delete" params="[courseId:course.id]">
        Delete
    </g:link>
</div>

在此代码块中,通过单击 删除 链接 参数 params="${[id:course.id]} ,它将重定向到 删除 内部的操作 登录 Controller 。在删除操作中打印参数,您将得到输出为
参数:[courseId:2,操作:删除,格式:null, Controller :登录]
在这里你可以看到你的 params.courseId 只是一个 长值不是列表。

希望它能帮助你理解你的问题。

关于Grails - 如何将一个 ID 从 View 发送到 Controller (多对多关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497131/

相关文章:

c# - 使用 GraphDiff 更新多对多关系会导致错误

ruby-on-rails - 向 Rails 3 中的多对多表添加另一个属性

c - 将数组作为参数传递给 C 中的函数

c++ - 如何修改按值传递的原始变量的内容?

python - 如何将参数传递给在 docker 容器内运行的 python 脚本?

grails - 有没有办法用 mongodb 控制 gorm 中的加载关系?

c# - MVC 4 - 多对多关系和复选框

spring - Grails是否默认使用Spring Dependency Injection

spring - 如何在 Grails Spock 单元测试中初始化/连接 bean?

Grails 服务器推送