1

This is a javascript/jQuery function . Here i am accessing grails session variable from javascript

 function changeContactPersonDetails(value){
    alert(value)
    //alert(typeof(value))
    $("#billingPersonName").val("${session.traineeDetais.name[" + value + "]}");
 }

here i want to use value of a variable "value" as index . but it gives error

 Caused by: groovy.lang.MissingPropertyException: Exception evaluating property ' + value + ' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property:  + value +  for class: java.lang.String
    at E__Workspace_opaltpm_grails_app_views_trainingOrder__billingDetailsTemplate_gsp$_run_closure1.doCall(E__Workspace_opaltpm_grails_app_views_trainingOrder__billingDetailsTemplate_gsp:15)
1
  • 1
    dude, i am not sure about grails, but in jquery part you have enclosed the ${session.traineeDetais.name[" + value + "]} with double quotes, may be this is causing the problem, Ex: if you want to use other language in jquery like php, it will be like $("#billingPersonName").val(<?php echo session.traineeDetais.name[" + value +"] ?>); Commented Sep 3, 2013 at 6:52

1 Answer 1

3

You are mixing server- and clientside code.

The variable value is the parameter of your (clientside) jquery function.

The following code ${session.traineeDetais.name[" + value + "]} is executed by grails (serverside), where the variable value is unknown.

You have to pass the array to your client, that you could access it within your jquery functions.

Use JSON to pass the data like this:

var dataFromServer =  ${session.traineeDetais.name as grails.converters.JSON}

function changeContactPersonDetails(value){
    alert(value)
    $("#billingPersonName").val(dataFromServer[value]);
}

A much cleaner way would be to compute/convert your array within the controller and pass it to the view ...

Sign up to request clarification or add additional context in comments.

4 Comments

getting this error: Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <g:render>: Error evaluating expression [session.traineeDetais.name as grails.converters.JSON] on line [7]: Cannot cast object '[Ljava.lang.String;@1c42b98' with class '[Ljava.lang.String;' to class 'grails.converters.JSON' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: grails.converters.J SON(java.lang.String, java.lang.String)
Instead of creating the dataFromServer JSON within your view, you should create that JSON Array within your action and pass it as a variable within your model - just for sepration of logic and layout ...
It makes no sense to edit your comments and replace it with another question... I don't know the type of content you are saving within your session. I have assumed that session.traineeDetais.name is an array. It looks like it is just a string instead of an array. Check your variables first.
The given code is just an example. You have to figure out how to adapt to your specific use case or give more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.