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 4118774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:00:16+00:00 2026-05-20T23:00:16+00:00

My controller is passing through a list which I then need to loop through

  • 0

My controller is passing through a list which I then need to loop through and update every record in the list in my database. I’m using ASP.NET MVC with a repository pattern using Linq to Sql. The code below is my save method which needs to add a record to an invoice table and then update the applicable jobs in the job table from the db.

public void SaveInvoice(Invoice invoice,  IList<InvoiceJob> invoiceJobs)
{
    invoiceTable.InsertOnSubmit(invoice);
    invoiceTable.Context.SubmitChanges();

    foreach (InvoiceJob j in invoiceJobs)
    {
        var jobUpdate = invoiceJobTable.Where(x => x.JobID == j.JobID).Single();
        jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
        invoiceJobTable.GetOriginalEntityState(jobUpdate);
        invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);
        invoiceJobTable.Context.SubmitChanges();
    }
}

**I’ve stripped the code down to just the problem area.

This code doesn’t work and no job records are updated, but the invoice table is updated fine. No errors are thrown and the invoiceJobs IList is definitely not null. If I change the code by removing the foreach loop and manually specifying which JobId to update, it works fine. The below works:

public void SaveInvoice(Invoice invoice,  IList<InvoiceJob> invoiceJobs)
{
    invoiceTable.InsertOnSubmit(invoice);
    invoiceTable.Context.SubmitChanges();

    var jobUpdate = invoiceJobTable.Where(x => x.JobID == 10000).Single();
    jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
    invoiceJobTable.GetOriginalEntityState(jobUpdate);
    invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);
    invoiceJobTable.Context.SubmitChanges();
}

I just can’t get the foreach loop to work at all. Does anyone have any idea what I’m doing wrong here?

  • 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-20T23:00:16+00:00Added an answer on May 20, 2026 at 11:00 pm

    It seems like the mostly likely cause of this problem is that the invokeJobs collection is an empty collection. That is it has no elements hence the foreach loop effectively does nothing.

    You can verify this by adding the following to the top of the method (just for debugging purposes)

    if (invoiceJobs.Count == 0) {
      throw new ArgumentException("It's an empty list");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the controller context depends on some asp.net internals. What are some ways to
My Controller class is decorated with an AuthorizeAttribute to protect the actions: [Authorize(Roles =
I want my controller to return the right HTTP response code when the user
I came across a controller in an older set of code (Rails 1.2.3) that
Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
I have a controller with an action method as follows: public class InventoryController :
I have a namespaced controller for some admin functionality. My create form does not
In my Rails controller, I'm creating multiple instances of the same model class. I
In my view controller's -viewDidLoad method, I call [myTextField becomeFirstResponder]; This works like a
I have a controller action that is being executed by a link that was

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.