0

I've a ASP.Net MVC 5 Application where I'm adding some data to the database. But instead of adding each one at a time, I want to add all the data that I want and at the end save all those entries to the database. But in between I want to list those provisional entries.

For example:

  • Open the page with my empty list (for now) and the Add button;
  • Add something and comeback to the list;
  • In the list I can see that entry but is not still in the DB;
  • At the end save all the entries to DB;
  • In another session If I come back to this, I see those entries and can manage them the same way (having a general save and not individual);

Something like this:

public static void AddEntry(Entry e)
{
    using (DBContext ctx = new DBContext())
    {
        ctx.Add(e);
        //without Saving changes
    }
}

public static void AddAll()
{
    SaveChanges();
    //To add all
}
micstr
  • 5,080
  • 8
  • 48
  • 76
Andre Roque
  • 503
  • 1
  • 9
  • 31
  • Why not just dynamically add controls to your view so that you can create multiple items and save everything in one action? –  Aug 19 '16 at 09:01
  • how is that? could you explain a bit more? – Andre Roque Aug 19 '16 at 09:02
  • Refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options for dynamically creating collection items in a view. –  Aug 19 '16 at 09:03
  • But In some cases I could have a mix of Database items and "context" items, because I could remove 1 loaded from the database and add another – Andre Roque Aug 19 '16 at 09:15
  • Sorry, not entirely sure what you mean by the last comment. If you wanted to remove 'existing ' items, then you could simply use a view model with a property (say) `bool IsDeleted` and bind it to a checkbox (or use jQuery to set it if you have a 'delete' button for the row to mark it for deletion when you submit the form. –  Aug 19 '16 at 09:21
  • basically at some point I could have a mix of "database entries" with "local entries" – Andre Roque Aug 19 '16 at 09:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121330/discussion-between-stephen-muecke-and-andre-roque). –  Aug 19 '16 at 09:31
  • 1
    Don't depend on feeble client state or session data! Use a staging table in which you can store temporary items (which the first method would do) and from which you can promote to the "real" items (a process that the second method would execute). Now if for some reason the client dies, you still have the staging records. – Gert Arnold Aug 19 '16 at 09:52
  • Use Dependency Injection for injection of DbContext per request scope – Mohammad Akbari Aug 19 '16 at 10:50
  • this does not sound like a good idea. First of all, is your DbContext a unit of work structure, solely as a "translater" between you and the database. When you remove from the context, you cannot be sure the entry won't be deleted from the database (depending on configuration and deletion strategy). When you receive from the context, you might have a mix between database items and cached items, and I don't even want to think about what errors await there... – DevilSuichiro Aug 19 '16 at 15:14

0 Answers0