0

I have a partial view that generates a row of controls based on the selection from a dropdown. For example if the user selects 2 from the dropdown my for loop generates 2 rows of fields for them to fill out.

 @for (int i = 0; i < 3; i++)
                        {
                            <div class="form-group col-md-3 col-sm-12">
                                Draw Number                                    
                                @Html.TextBoxFor(x => x.DrawSchedule.drawNumber, new { @class= "form-control input-sm" })
                            </div>
                            <div class="form-group col-md-3 col-sm-12">
                                Draw Amount
                                @Html.TextBoxFor(x => x.DrawSchedule.drawAmount, new { @class = "form-control input-sm" })
                            </div>
                            <div class="form-group col-md-3 col-sm-12">
                                Draw Date
                                @Html.TextBoxFor(x => x.DrawSchedule.drawDate, new { @class = "form-control input-sm" })
                            </div>
                            <div class="clearfix"></div>
                        }

I then have my repository to save to the db.

public void SaveContract(ProjectViewModel project, tblPMContract contract, tblDraw draws)
    {           

        tblPMContract dbEntry = repository.tblPMContracts.Add(contract);
        {                
            dbEntry.penaltyAmount = project.ProjectContract.penaltyAmount;
            dbEntry.estimateID = project.ProjectContract.estimateID;
            dbEntry.requestID = project.ContractorEstimate.requestID;

        }

        tblDraw dbEntry2 = repository.tblDraws.Add(draws);

        {
            dbEntry2.drawNumber = project.DrawSchedule.drawNumber;
            dbEntry2.drawAmount = project.DrawSchedule.drawAmount;
        }

        repository.SaveChanges();
    } 

I am not sure how to loop through a save to the database. If there is only 1 row of controls not a problem, but when there is more than 1 row it only saves the first row.

HHPRy
  • 17
  • 7
  • Your model needs to be a collection. Are you wanting to create and save 3 `tblDraw` objects or do you want to dynamically generate `tblDraw` objects (i.e could be any number of `tblDraw` objects)? –  Jan 08 '16 at 20:24
  • There is something wrong with your repository code here. Can you check and correct it for better understanding on what's going on here? – Andrew Jan 08 '16 at 20:32
  • dynamically generate tblDraw objects – HHPRy Jan 08 '16 at 20:32
  • @Andrew the repository code does save. Where I am stuck is if I have more than 1 tblDraw object, how to loop through and save them to the database. – HHPRy Jan 08 '16 at 20:35
  • @HHPRy, actually I asked you about correctness of your code which you provided here - its very messy, not to repeat your problem (and I understand but) – Andrew Jan 08 '16 at 20:43
  • If you wanting to dynamically add (and delete) `tblDraw` objects in the view, refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some examples. –  Jan 08 '16 at 20:48
  • @StephenMuecke thank you. I think that is exactly what I was looking for. – HHPRy Jan 08 '16 at 21:02
  • @Andrew I am new to mvc, can you explain to me so I understand what you mean by the repository code is messy? – HHPRy Jan 08 '16 at 21:03
  • OK forget about that. Why you can not use `foreach` loop and in each iteration you add current item of input collection to database collection. And after all that done call `SaveChanges` – Andrew Jan 08 '16 at 21:09

0 Answers0