views:

984

answers:

4

Hi everyone!

I have the following situation... In a certain View, the user must select the initial hour, the final hour and the weekday. But, I can't save this informations to DB 'cause I need to save my whole page and I need the primary key of the primary table, but that's not the point.

So, while I don't save these data to DB, I'm saving to a Session. I was told to save to a cookie, but it appears that cookies have a size limit. So, I'm saving to a Session.

Buuuut, I was also told that I could save these informations (hours and weekday) to the user page, simulating a ASP.NET ViewState...

Does anyone know how to do this?? Does anyone know how to save temporarily these data withou using cookie or Session??

Thanks!!

+14  A: 

Hidden input fields won't help?

<%= Html.Hidden(...) %>

Update (serializing an object to base64):

var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, myObject); // myObject should be serializable.
string result = Convert.ToBase64String(stream.ToArray());

When you want to fetch it back:

var formatter = new BinaryFormatter();
var stream = new MemoryStream(Convert.FromBase64String(hiddenFieldValue));
var myObject = (MyObjectType)formatter.Deserialize(stream);

Make sure you validate the data stored in the field when you use it as the client might change it. ViewState takes care of this automatically.

Side note: ASP.NET uses LosFormatter instead of BinaryFormatter to serialize ViewState as it's more efficient or ASCII based serialization. You might want to consider that too.

Mehrdad Afshari
I'm using hidden fields to some things... but how can I save a list of objects to a hidden field??
AndreMiranda
Binary serialize the object and convert the byte[] to base64 string.
Mehrdad Afshari
How can I do this?
AndreMiranda
Can you give me an example?
AndreMiranda
Mehrdad, I've tried to do that but threw an error saying that my entity is not marked as serializable. I'm using LINQ to SQL.
AndreMiranda
You can mark the entity class as `[Serializable]`.
Mehrdad Afshari
By the way I suggest you not to serialize the entity class. Create a separate class with appropriate fields you need and make that serializable. In the final step, construct an entity based on the data in that class and submit it to DB.
Mehrdad Afshari
In the code file associated with the DBML you can create a partial class that matches your entity and mark it as Serializable. However, as I suggested in my last comment, it's better to create a *new class* based on the data you want to store and make *that* [Serializable] instead.
Mehrdad Afshari
This is becoming confusing hehehe!For example, I have this entity SchedulePeriod and the fields hourInitial, hourEnd and weekday...
AndreMiranda
My last comment was your answer about makeing another class
AndreMiranda
Create your own class with those fields (forget the DBML classes). Mark that class as serializable. When you are ready to save it to DB, fill out a DBML class with data from your own. got it?
Mehrdad Afshari
And this 'string result' I will send to my hidden field, right?
AndreMiranda
Cai use this to save a List of objects, like IList<SchedulePeriod> ??
AndreMiranda
This works for all serializable objects. List<T> is serializable if T itself is serializable. So yes, if you can store SchedulePeriod, you can also store List<SchedulePeriod>. BTW, it depends on the actual type of IList. Nothing can be said about IList<T> interface in general. List<T> works though.
Mehrdad Afshari
For example, Mehrdad. User will select a initial hour, a final hour and a weekday and will click on 'save'. This will call a method that will serialize my new class that I created and pass it to a hidden. However, user will select another final and initial hour and a weekday and will save...
AndreMiranda
and this will also serialize my new class and pass it to the hidden field, but it will apss only the last 'save' action, am I right? The first 'save' the user did was lost, correct? That's why I've asked you about saving objects to a List.
AndreMiranda
Until now, what I was doing was to save a List to a Session and when user pick another initial and final hour and weekday, he will save these data and my method will check if Session already has a List. If so, this new data will be added to the List. But, Session is keeping what he is doing.
AndreMiranda
I think that if a serialize and save it to a hidden, it will save only the last 'save', because I will not have a place to keep the data entered before by the user...
AndreMiranda
Yes, this would work. You could also consider an AJAX style application that does all that stuff on the client and then submit it to server all at once.
Mehrdad Afshari
I think I will have to pass hidden field value as parameter of my method, check it has something in it, if so Deserialize it and save the last data entered to this List, Serialize it again and pass it back to my hidden...
AndreMiranda
I'm already using Ajax to save this data
AndreMiranda
Yes, you'll have to do that. You could avoid postbacks by also doing the selection and adding to the list with javascript and sending the whole data list to server.
Mehrdad Afshari
Does this 'string result' have a size limit??
AndreMiranda
2GB :) Warning: if your data is so much that you're thinking about size limit, ViewState and similar stuff are probably not the way to go.
Mehrdad Afshari
oww! 2GB?? My data is faaar below that! :-) I will test and tell you if it worked!
AndreMiranda
When I'm trying to deserialize it, it throws an error saying that my data lenght is invalid... it encrypts fine (at least, it seems), but when deserialize, it gives an error
AndreMiranda
I gave up... ti says the data length is invalid...nevermind, I will try this in another time.But, thanks for the help... :-)
AndreMiranda
A: 

You could save a javascript array on the client... and then transmit all the information when the user ultimately saves.

You have to work a little more, but in the end it pays off.

I heavily use jQuery to do stuff like that, it's easier than it seems.

Juan Manuel
Juan, I'm also using jQueryin the project. The problem is that this data is saving to a Session and I'm using jQuery Flexigrid to show them. My Session is feeding Flexigrid...
AndreMiranda
A: 

If you just want to save the data for that request and the next request I'd recommend using Tempdata, else I'd recommend using Mehrdad`s answer.

+3  A: 

TempData["MyData"], mind you this will only last one round trip.

Al Katawazi