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

  • SEARCH
  • Home
  • 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 8385069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:34:29+00:00 2026-06-09T17:34:29+00:00

In my controller I’m looping through items and saving them to my db. The

  • 0

In my controller I’m looping through items and saving them to my db. The problem is that it saves the first item, but none of the others. I put a breakpoint on the “SaveItem()” line in the loop and it hits it every time, but what seems odd to me is that it only goes through to the method for the 1st item.

What am I doing wrong?

public void SubmitItem(Cart cart, ShippingDetails shippingDetails, ProcessedItems processedItem, string orderID)
{
    var cartItems = cart.Lines;
    //CartIndexViewModel cartIndex = new CartIndexViewModel();
    //var customID = cartIndex.OrderID;

    foreach(var item in cartItems)
    {
        processedItem.OrderID = orderID;
        processedItem.ProductID = item.Product.ProductID;
        processedItem.Name = item.Product.Name;
        processedItem.Description = item.Product.Description;
        processedItem.Price = item.Product.Price;
        processedItem.Category = item.Product.Category;
        processedItem.ImageName = item.Product.ImageName;
        processedItem.Image2Name = item.Product.Image2Name;
        processedItem.Image3Name = item.Product.Image3Name;

        processedItem.BuyerName = shippingDetails.Name;
        processedItem.Line1 = shippingDetails.Line1;
        processedItem.Line2 = shippingDetails.Line2;
        processedItem.Line3 = shippingDetails.Line3;
        processedItem.City = shippingDetails.City;
        processedItem.State = shippingDetails.State;
        processedItem.Zip = shippingDetails.Zip;
        processedItem.Country = shippingDetails.Country;

        processedItem.Status = "Submitted";

        processedItems.SaveItem(processedItem);

    } 

}


    public class EFProcessedItemsRepository : IProcessedItems
{
    private  EFDbContext context = new EFDbContext();

    public IQueryable<ProcessedItems> ProcessedItem
    {
        get { return context.ProcessedItems; }
    }

    public void SaveItem(ProcessedItems processedItem)
    {
        if(processedItem.ProcessedID == 0)
        {
            try
            {
                context.ProcessedItems.Add(processedItem);

                context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }
        else
        {
            context.Entry(processedItem).State = EntityState.Modified;

        }
    }

    public void DeleteItem(ProcessedItems processedItem)
    {
        context.ProcessedItems.Remove(processedItem);
        context.SaveChanges();
    }
}

here is the class for the processedItem:

public class ProcessedItems
{
    [Key]
    public int ProcessedID { get; set; }

    public string OrderID { get; set; }
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
    public string ImageName { get; set; }
    public string Image2Name { get; set; }
    public string Image3Name { get; set; }
    public string Status { get; set; }

    //shipping
    public string BuyerName { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }


}

Interface:

public interface IProcessedItems
{
    IQueryable<ProcessedItems> ProcessedItem { get; }
    void SaveItem(ProcessedItems processedItem);
    void DeleteItem(ProcessedItems processedItem);
}
  • 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-06-09T17:34:31+00:00Added an answer on June 9, 2026 at 5:34 pm

    try calling context.SaveChanges() after adding all of the items, I think it should persist them all in one go.

    Another thing to try:

    Refactor your code so that SaveItem accepts only one item to save, Add it and call SaveChanges()

    Loop through the cart items outside the method and call the method with one item to save at a time.

    // set orderID, shippingDetails above
    
    foreach(var item in cartItems)
    {
        ProcessedItems processedItem = new ProcessedItems();
        processedItem.OrderID = orderID;
        processedItem.ProductID = item.Product.ProductID;
        processedItem.Name = item.Product.Name;
        processedItem.Description = item.Product.Description;
        processedItem.Price = item.Product.Price;
        processedItem.Category = item.Product.Category;
        processedItem.ImageName = item.Product.ImageName;
        processedItem.Image2Name = item.Product.Image2Name;
        processedItem.Image3Name = item.Product.Image3Name;
    
        processedItem.BuyerName = shippingDetails.Name;
        processedItem.Line1 = shippingDetails.Line1;
        processedItem.Line2 = shippingDetails.Line2;
        processedItem.Line3 = shippingDetails.Line3;
        processedItem.City = shippingDetails.City;
        processedItem.State = shippingDetails.State;
        processedItem.Zip = shippingDetails.Zip;
        processedItem.Country = shippingDetails.Country;
        SubmitItem(processedItem);
    
    }
    
    public void SubmitItem(ProcessedItems processedItem)
    {
        processedItem.Status = "Submitted";
        processedItems.SaveItem(processedItem);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Controller First I tried this: [HttpPost] public ActionResult Edit(JournalEntry journalentry) { if (ModelState.IsValid) {
My controller get datas from database model, and sends them to view. In view
setup Controller -> Action Index results = Model.all (SQL Call) View Loop through and
Controller code: public function searchAction() //search action for items { $form = new Application_Form_Search();
The controller should ideally be free from domain logic right? How does that match
controller code that doesn't work: @mailings = Mailing.find(:all, :conditions => [created_at.month = ?, m])
@Controller @RequestMapping(/User.html); @sessionAttributes(user); class something Now i know that @Controller = implements Controller @requestMaping
My controller is passing through a list which I then need to loop through
My controller is decorated with [HandleError] and [HandleError(ExceptionType=typeof(CustomException), View=CustomView)] . I have views that
Controller: @micropost = Micropost.new(params[:micropost]) But this form_tag is sending me params[:content] instead of params[:micropost][:content]

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.