0

I'm working with creating a partial view to allow the end user to add an item for a later submission.

I have the following class defined as a ViewModel:

    public class AddItemsContext : DbContext
{
    public DbSet<Item> Items { get; set; }
}

Connection string:

I then have an AJAX call that uses this function to add an item to my partialview:

        [HttpPost]
    public ActionResult Index_AddItem(SearchedItem viewModel, string Text, string Value)
    {

        AddItemsContext db = new AddItemsContext();

        string test = Request.Form.GetValues("cbxPerm")[0].ToString(); // to be used later
        Item NewItem = new Item();
        NewItem.ItemName = "test"; // just set it to a temp var for testing

        db.Items.Add(NewItem);
        db.SaveChanges();

        return PartialView("_AddItems", db.Items);
    }

The item list does build in my partial view but then I noticed the list persists even after stopping and restarting my app. I thought it may be writing in the SQL DB but I don't see it there either.

I found this: where is data stored? but I don't have any other datasource defined other than my main app's DB.

I also read this: DbContext disposing? which appears to be saying that the data should be disposed on exit.

This partial view needs to add/remove items before the form is submitted for a DB write.

I'm trying to understand why/where the data is being stored permanently?

Community
  • 1
  • 1
Zonus
  • 2,313
  • 2
  • 26
  • 48
  • Have you looked in the `App_Data` directory of your website for an `.mdf` file? Can you post your connection string? On a separate note, unless your `Items` table is going to contain a very small number of items, you're going to run into scale problems pretty quickly trying to return all the items in the table back to the view - you're effectively passing `SELECT * FROM Items` to the view. You're also not disposing your `DbContext`, which will cause database connection leaks, and likely `ObjectDisposedException` (or similar) later when the `db.Items` property is accessed by the view. – Martin Costello Apr 28 '14 at 12:41
  • There will be no more than 10 or so items added into the view. The only .mdf file I have in there is aspnetdb.mdf but the date/time is from 10 months ago. Not even sure what that is; I'll look into that later. This is new code; I'm just trying to get the list to show and thought I had it working until I noticed the list persisted between debug sessions.... This is my connection string: – Zonus Apr 28 '14 at 12:55
  • Based on that connection string, it looks like your data is being stored in the SQL Server instance named `SQLExpress` on the local machine to where the website is running in a database called `MyApp`. – Martin Costello Apr 28 '14 at 12:57
  • Ah, on closer examination I see it created a new DB called: MyApp.ViewModels.AddItemss with a table called dbo.Items. I'm not sure I want this to happen perhaps I'm going about this the wrong way.... I was hoping to maintain/pass a list in the partial view; not storing it in a DB permanently until the form submits. But then, it needs to be stored in my real db. – Zonus Apr 28 '14 at 13:01
  • @Scott, What do you mean writing: 'I noticed the list persists'. How do you recognize it. Where does the list persist? (in the view, memory?) – Bronek Apr 28 '14 at 14:43
  • The list keeps showing after I stop debugging and restart the app in a new debugging session. End program/restart. The data from previous sessions shows up again. – Zonus Apr 28 '14 at 17:11

0 Answers0