Jack
Jack

Reputation: 104

C# MVC foreach loop Problem

I am trying to find ways to do multiple record update using foreach loop similar to snippet below. This snippet doesn't work on value type. If anyone would like to share their experience of multiple record update in MVC Framework. Thanks in advance

public ActionResult UpdateProductPrice()
{
   var products = _rep.GetProducts();

   foreach (Product p in products)
   {
       decimal oPrice = p.Price;
       p.Price = oPrice * .15

       UpdateModel(p);
       _rep.Save();
   }

   return View("Index");
}

Upvotes: 2

Views: 964

Answers (1)

Robert Groves
Robert Groves

Reputation: 7738

You are calling UpdateModel after you've changed p.Price. If the controller's current value provider has a property that maps to Price your changes will be lost during the UpdateModel call.

Upvotes: 1

Related Questions