Neo
Neo

Reputation: 16239

Instead of Session variable where i can save my temp data in mvc?

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.

Upvotes: 0

Views: 10163

Answers (3)

kingdango
kingdango

Reputation: 3999

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.

Upvotes: 5

viperguynaz
viperguynaz

Reputation: 12174

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
    }
}

Upvotes: 2

dom
dom

Reputation: 6832

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"] = "";

Upvotes: 2

Related Questions