Redirect based on page source - grails

I have an edit page, which can be accessed from 2 list pages which are page A and page B (both performing about the same thing but is totally different). The edit is a part of the page B controller, and it has a delete function to delete any item from A or B.
The thing is, whenever i delete an item from page A or B, it will return to page B. This makes sense anyway since the edit page controller is the page B controller.
So how can i make it so that if the item is from page A, it will return to page A after delete, and if its from B, it will return to page B? I've been thinking of using 2 delete functions on page B controller but i guess that's not the effective way to handle this problem.

Related

MVC Model empty after posting second form back to same controller

I have page that has 2 forms posting back to the same controller
The first form posts back and the result sends back an updated model to allow the second form to appear on the page.
Concept is the user is shown items from a list they want to select to return.
HTML for this is as follows:
The page sends that form back to this controller
With this model
The 'GetModel' is the model that is past back to the page
Then sends back to the same page to allow a preview of what they want to return for the user to confirm.
When the second form as below:
is posted back the the same controller and function again model is completely empty but cant see why?

Page refresh in MVC is calling many methods in more than one controller

I have a index page for which the the layout page is different and there is another index page in a different folder under the same project. Both these index pages have one and two partial views in each and also have their own controllers, one is home and the other testController.
My problem is when I hit f5 in the browser, it is calling the partial view method and index method for both the controllers home and the testcontroller and it is clearing off certain variables. My understanding is that it must call only the index method of the testcontroller and call the partial views which is in this page and controller. Is my understanding wrong?
In other words, you have a view that calls child actions. When you use Html.Action or Html.RenderAction, for the most part, it's as if you went to those actions directly via a URL. Everything that needs to happen to route to an action, run it, and return a rendered view has to happen for a child action, just as with a normal action.
Where your question gets confusing is in what you're expecting to happen when you refresh the page. Refreshing the page will replay the request to the server, which must then create and send the response just as it did for the original request. That means, your main action and any child actions will be hit again. I'm not sure why you would think something else should happen. Perhaps, you're wanting to only refresh a portion of the page? That's where AJAX comes in, but it won't help you with something like pressing F5. That will always cause the full page to be reloaded.
I figured out the problem I had with both controller index methods being called. I was using the same layout page in both index pages. Point one of the index pages to the right layout page stopped calling the Home controllers index method.

Displaying Grails domains in jquery tabs

What would be the best way to display data from Grails database in JQuery UI tabs? What I would like is to have a tab interface and on each tab is a list of the records from a different domain. For instance, Tab1 displays the record list from Domain1, Tab2 displays the record list from Domain2, etc.
I have the JQuery UI tab interface set up and working and am currently using createLink to call the method from the controller to return the model of the appropriate domain. The tabs look like this:
<div id="tabs">
<ul>
<li>Hardware records</li>
<li>Model records</li>
<li>Building records</li>
</ul>
</div>
The method from the controller looks like this:
def listHardware() {
[hardwareList:Hardware.list(), hardwareInstanceTotal:Hardware.count()]
}
I've also played around with rendering a whole GSP within the tab by using "render(view:'HardwareList', model:[hardwareList:Hardware.list(), hardwareInstanceTotal:Hardware.count()]", but that takes a VERY long time (at least 5 seconds) to load each time the tab is selected and is not at all ideal, especially if it were to take that long for each tab.
UPDATE
As noted in one of my answers to Rimero's answer below, I was able to use templates to display tables of my domains' data. I'm also trying to implement pagination on each tab using the tag, but each time I click on one of the pages to view another page, it takes me to the full template itself outside of the tab interface. Any thoughts on how to format the tag so that everything stays within the tab??
Here's my suggestion:
You can fetch everything at once in your controller in your index method for example.
You can implement your tab contents as templates
(g render template). Each tab == 1 template.
You can fetch your domain objects buildingList,
etc. from the index method of your controller.
The g:render template code for each tab may only need to be passed a map or a collection for rendering.
In this case you don't need hyperlinks to controllers endpoints. You just keep anchors to the tab(div id) as in the default example here -> http://jqueryui.com/tabs/.
UPDATED ANSWER
As you said that sending all the data at once takes a long time, you could fetch it asynchronously. If possible populate the data only for the first tab directly.
Create a business method for each tab, that will return the model as JSON, data is only fetched if not already retrieved from the server (Need to keep state or see for example if the tab id has some DOM nodes.
Using JQuery, when the DOM is ready, get the current tab and if you didn't fetch the data for the first tab eagerly, fetch it at this moment with the busy image spinning.
As soon as you select a new tab, you need to check if the data was already fetched, if not, you send an ajax call and your callback function populate the data in the tab div container for example.
Hope it helps.

How to get last page visited in a controller

is there a way to get or store the last page visited? Example if I'm on a List Page with a New link that loads a page with form. If the user cancels, you go back to the previous page (List Page).
Thanks
Well, in web forms, you would use: Request.UrlReferrer
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx
So I assume you can also use that in MVC, accessible through the HttpContext property of the RequestContext acessible by teh controller.

Asp.net MVC eCommerce app - Add To Cart implementation

Let say I am rendering list of products with Add To Cart link / button next to each.
I also have a CartController with AddToCart Action that accept a parameter of Product type.
After product is added to cart user should stay on the same page (Product list) - not to be redirected to Cart or something.
I am rendering Cart summary partial view that should update itself.
So my question is about implementation of the Add To Cart link / button.
Should I use: Html.ActionLink(...)
or Html.BeginForm() and Submit button.
Maybe there is other ways...
How do I send Product info in each case?
Thanks
I suggest using a jQuery.post() request to your CartController.AddToCart. Make AddToCart a partial view. And then show a modal popup (div on top of your page) that shows that the product was added to the cart...then do a fade out!
Keep it simple.
Now keep in mind that some of your users won't be able to support jquery/javascript. So make the initial add to cart button post to an add to cart page (not partial page..full page)...which can return them to the original page. Then spot weld your jquery function on top of the add to cart button. This way you cover both worlds nicely.
Take a look at the concept of unobtrusive javascript/jquery.
The way I do it is to have a form for each add button (with maybe the quantity also), since you want your AddToCart action only receive POST actions anyway, that way you process all the add to cart logic and then redirect to your main catalog view (or something like that :)
Again I'd take into consideration what happens if the user doesn't have javascript. It's not very likely these days, but do you want to lose a sale just because someone accidently turned off javascript?
So what I'd do is create the somewhat typical AddToCart link (<a class="add" href="/Shop/AddToCart/5">). The AddToCart controller would then do it's stuff and redirect back to the product listing page (you might have to pass a parameter to specify which page to go back to).
So that's the first step to consider -- javascript turned off. Now you can think about how to do it with javascript.
Capture the click event ( $('a.add').click(...) ) and then you can do two things. One would be to call the URL (you can get it out of the event object) and then separately update the cart. You can also add a parameter to the URL so that it displays the cart partial view, doing something like (written from memory):
$('a.add').click(function(event) { $('#cart').load(event.target.attr('href') + '&showcart=1');
James

Resources