Mike Caputo
Mike Caputo

Reputation: 1156

Grails call Controller method from View

I have a quick question for grails. I have a view page that iterates over an array of objects to display them. The HTML/Grails tag code looks like this:

<g:each in="${ICFList}" status="i" var="icf">
<tr>
    <td>${icf.printName?.encodeAsHTML()}</td>
    <td>${icf.activeNote?.encodeAsHTML()}</td>
</tr>
</g:each>

This code works and displays what I need. However, I don't want to store a printName variable inside my icf object anymore. the object also contains a one character code to identify it, and I have a method that looks up the printName value based on the code. Basically, my question is this:

Is there a way to do something like this

<td>${icf.getNameFromCode(${icf.code})}</td>

without having to store the name anywhere? The getNameFromCode method is defined in the controller object for this particular view, but I can't figure out how to access it from the gsp. Any help is greatly appreciated.

Upvotes: 1

Views: 7024

Answers (3)

Victor Sergienko
Victor Sergienko

Reputation: 13475

Answering the question formally, you can use g:include:

<g:include controller="book" action="list" />

though the fields of single entity feel so much more like a Model.

Upvotes: 7

jjczopek
jjczopek

Reputation: 3379

It can also be achieved by making AJAX calls to this controller, but I think Gustavo's answer is what you are looking for.

Upvotes: 1

Gustavo Gir&#225;ldez
Gustavo Gir&#225;ldez

Reputation: 2690

I don't think you can call a controller method from the view.

What I'd suggest is either to move the getNameFromCode to the domain class or implement the logic in a taglib and use a custom tag to display your data. Which option to choose depends on whether that method refers to something that's intrinsic to the model or something that's tied to the view.

The latest Getting started with Grails screencast explains how to create and use custom tags.

Upvotes: 4

Related Questions