views:

40

answers:

4

Im using MVC ASP.NET C#, jQuery

I'm building what could be decribed as the simpliest shopping cart in the world. Basically My Clients wants users to be able to browse the site, Click on a product they want and it be added to a "list" and then when they "Checkout" they simply fill in a form and it emails my client with the list of products they had chosen!

I was thinking of something like storing them in a cookie. So as the user browses they won't be lost, Then have a jQuery dialog appear when they choose to view/checkout their cart. and it can list all products and then they simply fill in a simple form..

Is this the best way to go about it. Its a cheap website and I would like the simplest way to do this? All i guess I would need to sort is the product Id's..

Any ideas of better ways or any opinions at all!

+1  A: 

I'd say to go for the Session object. You can always configure the location of Sessions at runtime

I think some may dislike this storage method (it breaks testing isolation, if i'm not mistaken), but it's there for free :)

samy
+1  A: 

Another option would be to store it in the users session. A benefit of this is if the user has cookies turned off and the site caters for cookieless session state then they will still be able to select products and checkout.
The thing to look out for is how much you could potentially end up storing in session. From the sounds of it this will not be an issue but if this could potentially use up an unacceptable amount of memory then you would probably need to consider a database approach anyway rather than cookies.

Andy Rose
+2  A: 

Using Session depends on whether you think the users will pick the products in one go. Or will they leave the page and come back in an hour? The problem being that if they come back in an hour, the Session State may have been garbage collected to free up resources on the server, or the session might have expired.

To get around this, if all the products are on one page, you could store the chosen products in a hidden field, encrypted and all, that will stay there until the user closes the app.

You just need to serialize the list of product Ids, pass that serialized string to the view and put it in a hidden field.

awrigley
+1  A: 

If you're using jquery, you could store the basket as a json array inside the $('body').data() element (or actually as i do, under a div called '#storage'). this works as a fantastic local storage mechanism, tho' would only be relevant to the current page that the user was on and would be cleared on moving to subsequent 'new pages' unless of course, your design was such that the shopping page was ALWAYS the same page and only refreshed by ajax methods. this way, you could continually append/modify the json structure on the 'worksurface' page.

i use this technique for a different application of the logic, but virtually for the same reason.

here's a snippet of the kind of thing i do:

/* example of data params key*/
var keyParams = "Data-Search-type-" + $('#searchtype').val();
/* add json to body with key*/
$('#storage').data(keyParams, jsonData);

/* get same data back later */
var jsonData = $('#storage').data(keyParams);

When i 'save' the data to the server, i then clear the data() element back to null. There is of course the other option of localstorage itself which can be used well, especially in disconnected environments (such as mobile apps).

another way to skin the many skinned cat!!

jim
I do intend to have it persist across a number of pages so storing it in javascript is not really a option!
Steve
yes, this is for a very specific scenario in mind, so page level would require a redesign if you're moving across different pages. my scenario has a base 'workspace' that gets populated by different ajax partials, so is a different approach. hope you find your 'fix' ;)
jim