0

I have created a mvc3 application. Currently saving submit form value into Session as i need to do some stuff on that before save changes to database.

Is there any other good way to store temp data in mvc like ViewState?

I'm saving form input values into Session variable each time when data is entered and and when I clicked on submit button.

After every submit i'm populating my session variable with new added values and displaying all those values into webgrid.

Once i'm done with insertion finally clicked on update button which takes all values from webgrid and pass to one stored procedure as a parameter.

Now here i want to achieve 2 things
1.need to save all inserted data as clicked on submit it gets refresh all time.
so need to save all previous data to.
2.currently using session variable which sets to webgrid to show data into webgrid and once done with insertion passing those values to stored procedure.

So need to know any other good way to save data.

Else can use javascript to do this all on client side.
9
  • The question is: How long do you want to store that data and who should access that data? Commented Apr 19, 2012 at 15:47
  • about 1minute i need that data .. as i need to pass that data to one stored procedure... Commented Apr 19, 2012 at 15:50
  • 1
    I think either you process and save your data in the web request, then there is no need to store it somewhere else or you do your data processing in some asynchronous background task. Then you can pass your data to that background thread. Commented Apr 19, 2012 at 15:56
  • 1
    That is not specific to mvc3 but to web apps in general. You have to tell us more details of what exactly you want to do with the posted data and why its not possible to do that while processing the current web request. Commented Apr 19, 2012 at 16:07
  • 1
    You should avoid schemes that attempt to make the stateless web seem stateful for two reasons. I 13 years I've seen session used almost exclusively for the wrong things -- developers regress to using session for convenience and later regret it. Commented Apr 19, 2012 at 16:28

3 Answers 3

5

You should avoid schemes that attempt to make the stateless web seem stateful for two reasons.

Firstly, Session DOES NOT avoid a round-trip, it's only nominally different than storing values in the database. Yes, a fetch of memory is faster than a fetch from SQL but this doesn't scale well -- to scale (more than 1 server) you must add components and hops that reduce the gains and dramatically increase the complexity.

Secondly, Session values are likely to make it to a database anyway so why not just start with them there in the first place.

Thirdly (yes, another one), Session is not necessarily something to trust any more than a hidden form field would be.

In 13 years I've seen session used almost exclusively for the wrong things -- developers regress to using session for convenience and later regret it (as do the developers who replace them and have to maintain their code).

In short, use a database to store this information and be smart about how you fetch and cache it.

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

2 Comments

gr8ly explained :) i can't use table from database thats why i need something to store mt temp data :( please suggest something else rather than session
Use the ViewData object and persist it yourself between posts. On each post include a hidden field that contains your ViewData.
2

You can use TempData (http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.tempdata.aspx), but know that it persists only from one request to the next.

TempData["someKey"] = "";

1 Comment

i need to save that values as i'm showing that in my webgrid so inserting new values will update a webgrid.. No luck with TempData in that case
2

if you need to persist the data across posts, Session is the proper place. If you need to write the data to a database, then the model should handle the data on post. In your controller:

[HttpPost]
public ActionResult Index(YourModel model)
{
    if (ModelState.IsValid)
    {
        model.SaveToDataBase();
        return View("Success");
    }
    return View("Index", model);
}

Then in the model:

[Serializable]
public class YourModel
{
    [Required]
    public string YourData { get; set; }

    public void SaveToDataBase()
    {
         //TODO - add code to call stored procedure with data posted to model
    }
}

Comments

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.