Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6595661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:58:08+00:00 2026-05-25T17:58:08+00:00

Why wont my changes persist to db after I click save. Product Repository: public

  • 0

Why wont my changes persist to db after I click save.

Product Repository:

public class ProductRepository
{
    NorthwindDataContext context = new NorthwindDataContext();

    public IList<EditableProduct> All()
    {
        return (from product in context.Products
                select new EditableProduct { 
                    ProductID = product.ProductID,
                    ProductName = product.ProductName,
                    UnitPrice = product.UnitPrice.HasValue ? product.UnitPrice.Value : default(decimal),
                    UnitsInStock = product.UnitsInStock.HasValue ? product.UnitsInStock.Value : default(int),
                    Discontinued = product.Discontinued,
                    LastSupply = DateTime.Today
                }).ToList();
    }

    public EditableProduct One(Func<EditableProduct, bool> predicate)
    {
        return All().Where(predicate).FirstOrDefault();
    }

    public void Update(EditableProduct product)
    {
        EditableProduct target = One(p => p.ProductID == product.ProductID);

        if (target != null)
        {
            target.ProductName = product.ProductName;
            target.UnitPrice = product.UnitPrice;
            target.UnitsInStock = product.UnitsInStock;
            target.Discontinued = product.Discontinued;
            target.LastSupply = product.LastSupply;
        }
    }

    public void Insert(EditableProduct product)
    {
        product.ProductID = All().OrderByDescending(p => p.ProductID).First().ProductID + 1;
        All().Insert(0, product);
    }

    public void Delete(EditableProduct product)
    {
        EditableProduct target = One(p => p.ProductID == product.ProductID);
        if (target != null)
        {
            All().Remove(target);
        }
    }
}

Controller:

public partial class GridController : Controller
{
    ProductRepository productRepository = new ProductRepository();

    public ActionResult EditingBatch()
    {
        return View();
    }

    [HttpPost]
    [GridAction]
    public ActionResult _SelectBatchEditing()
    {
        return View(new GridModel(productRepository.All()));
    }

    [HttpPost]
    [GridAction]
    public ActionResult _SaveBatchEditing([Bind(Prefix = "inserted")]IEnumerable<EditableProduct> insertProducts,
        [Bind(Prefix = "updated")]IEnumerable<EditableProduct> updatedProducts,
        [Bind(Prefix = "deleted")]IEnumerable<EditableProduct> deletedProducts)
    {
        if (insertProducts != null)
        {
            foreach (var product in insertProducts)
            {
                productRepository.Insert(product);
            }
        }
        if (updatedProducts != null)
        {
            foreach (var product in updatedProducts)
            {
                var target = productRepository.One(p => p.ProductID == product.ProductID);
                if (target != null)
                {
                    target.ProductName = product.ProductName;
                    target.UnitPrice = product.UnitPrice;
                    target.UnitsInStock = product.UnitsInStock;
                    target.LastSupply = product.LastSupply;
                    target.Discontinued = product.Discontinued;

                    productRepository.Update(target);
                }
            }
        }
        if (deletedProducts != null)
        {
            foreach (var product in deletedProducts)
            {
                productRepository.Delete(product);
            }
        }
        return View(new GridModel(productRepository.All()));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:58:08+00:00Added an answer on May 25, 2026 at 5:58 pm

    You seem to be using the code from our sample application which indeed does not update the database. It merely updates the data in memory so every single user browsing the demos sees only his changes.

    To make it work you need to update the database. The actual implementation depends on the framework you are using for data access. For example for Linq To SQL you should use the SubmitChanges method. You can check this code library project which updates the underlying database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out why the binding changes in my large project wont work.
firefox wont allow the javascript to save a file in client machine is the
I wont to do insert to one mysqll server to and get the data
strtok wont work correctly when using char *str as the first parameter (not the
The values wont insert into my database, the message tells me that registration is
First off tags wont work. I say this because i create 4 buttons all
I have a model that members will be able to update but their changes
I have a DataTable and when I alter (add,modifiy..ect) it wont reflect on the
I can't figure out why this isn't working. It changes to the first link
I have an issue trying to persist entities to the DB using code-first technique.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.