Asp.net mvc 5循环控制并保存到数据库

时间:2016-01-08 20:13:45

标签: asp.net-mvc entity-framework

我有一个局部视图,根据下拉列表中的选择生成一行控件。例如,如果用户从下拉列表中选择2,则我的for循环生成2行字段供他们填写。

 @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>
                        }

然后我将我的存储库保存到数据库。

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();
    } 

我不知道如何循环保存到数据库。如果只有一行控件没有问题,但当有超过1行时它只保存第一行。

0 个答案:

没有答案