Kiran Chitturi
Kiran Chitturi

Reputation: 503

How to pass a map from gsp to controller as part of params in Grails remoteFunction

I am trying to send map from gsp to controller but the map is considered as a string in the controller

<g:remoteFunction 
action="updateCart" 
params="{startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:'${shoppingCart}'}" 
update="resourcesSelectedId"/>

Here the shoppingCart is a grails map variable I am trying to send

Edit:

there was a typo in the code I posted above. Missed starting "{" in the params

params="{startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:'${shoppingCart}'}" 

Updated my question as per my comments below

In my case shoppingCart is an object and it has, lets say for example, items and quantity of each item. I have some rules to be applied based on the items selected and quantity and determine the price for each item and show it back to the user. I want to do this processing the controller. Whenever user updates the cart I need to re-calculate and show it back to user. Is there any other better approach you would suggest to do this instead of passing the objects back and forth

Upvotes: 0

Views: 2224

Answers (2)

Kiran Chitturi
Kiran Chitturi

Reputation: 503

I could able to overcome this issue by following below steps

  1. converted the shoppingcart object to json string
  2. Pass the json as part of the params
  3. parse the json string server-side & process
  4. pass the updated object back to gsp

Upvotes: 0

Gregg
Gregg

Reputation: 35864

Anytime you're using the params attribute, it has to be in the format of a Map anyway, which means including the [ ]. This also means you can exclude the ${ } from any values because grails will parse all these as potential variables.

<g:remoteFunction 
action="updateCart" 
params="[startDate:stDt,endDate:endDt,cartId:pkid,shoppingCart:shoppingCart]" 
update="resourcesSelectedId"/>

However, keep in mind that you can't send objects. I'm not sure what shoppingCart is in your example, but it would only be able to be a simple value that can be represented as a String. Possibly you would want shoppingCart.id? Otherwise, that should get you going in the right direction.

Upvotes: 1

Related Questions