开发者

How to loop through and update multiple records at once in ASP.NET MVC Repository?

I'm having some trouble updating records in my SQLRepository, and I'm not sure how to implement this properly or if I'm just approaching this in completely the wrong way.

I have an invoice view which asks the user for a customer, start date and end date. Once submitted my controller then builds an IList of all the jobs which match the criteria and the view is updated via Ajax to display the list. On the same view, I have another field which asks the user to confirm these jobs are ok to invoice.

I need this second button to then add a record to my invoice table with the invoice data - this works fine. What I also need to do is update all the job records (in the IList) to include the invoice ID number. I can't seem to get this to work.

My updated method on the second button is below. I've tried multiple re-arrangements of this and nothing has worked. To be honest, I totally lack any sort of experience in this scenario (I'm not a developer by nature) so I'm not sure if I'm anywhere close to the mark with this.

public void SaveInvoice(Invoice invoice, IList<InvoiceJob> invoiceJobs)
        {
            foreach (var j in invoiceJobs)
                {
                    InvoiceJob o = j;
                    InvoiceJob jobUpdate = (from i in invoiceJobTable where i.JobID == j.JobID select i).Single();
                    jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
                    invoiceJobTable.Attach(jobUpdate);
                    invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);

                }
                invoiceTable.InsertOnSubmit(invoice);

            invoiceTable.Context.SubmitChanges();
            invoiceJobTable.Context.SubmitChanges();

        }

The invoice table is updated fine with this method and the code runs, but the InvoiceRef is not updated for the jobs in the invoiceJobTable. It doesn't seem to touch t开发者_开发知识库his. As far as I can tell, the invoiceJobs IList isn't null as if I change what data the controller sends through I get null exception error on the above foreach loop.

I hope I've explained this well enough. Any help is appreciated; I'm just a noob trying to learn. xD


have a look at this post;

How do I save multiple Models edited in form in ASP.NET MVC?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜