在for循环中动态添加'添加按钮'

时间:2017-10-02 09:49:36

标签: c# jquery asp.net-mvc

我有foreach循环,其中包含一些数据。我想要的是,如果用户单击添加按钮,将详细信息保存到数据库以及清空输入值,并转到foreach循环的下一次迭代。 以下是我到目前为止尝试的代码。

@model ModelLib.Models.PaymentDetails
@{
    Layout = null;
    var fullCount = Model.listRoomDetails.Max(x => x.FullCount);
}

<div class="row">
    @Html.AntiForgeryToken()

    <div class="row">

        <div>You have to add <span id="passangerCount">@fullCount</span> Passanger details</div>
         @foreach (var item in Model.listRoomDetails) { 
            <span>@item.RoomType RoomType Passanger Count : </span ><div id="@item.RoomType">@item.Count</div>
         }

        @foreach (var item in Model.listRoomDetails) { 
         <div class="row">
            Room Type - <span id="existing_RoomType">@item.RoomType</span>
            Room Category - <span id="existing_RoomCategory">@item.RoomCategory</span>

<div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-info" style="background-color:#ffffff;">
                        <div class="panel-heading" id="payment_heading" style="border-style: double; text-align: center; background-color: #f59e6b; border-color: black; color: black; font-weight: bold;">Add Payment Details<i class="fa fa-chevron-circle-down" style="font-size: 24px; float: right; margin-right: -17px; margin-top: 6px;"></i></div>
                        <div class="panel-body" id="payment_body">
                            <div class="row">
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.TicketNumber, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            @Html.TextBoxFor(model => model.TicketNumber, new { type = "text", maxlength = "13", @id = "ticketNumber", @class = "form-control", data_toggle = "tooltip", title = "Enter Ticket Name" })
                                        </div>
                                    </div>
                                </div>

                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.PassangerName, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        @Html.TextBoxFor(model => model.PassangerName, new { @id = "passangerName", @class = "form-control", data_toggle = "tooltip", title = "Enter passanger Name" })
                                    </div>
                                </div>

                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.PassportNumber, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        @Html.TextBoxFor(model => model.PassportNumber, new { @id = "passportNumber", @class = "form-control", data_toggle = "tooltip", title = "Enter passport number", maxlength = 8 })
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.PkgVal, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            @Html.TextBoxFor(model => model.PkgVal, new { type = "number", @min = "0", @id = "pkgVal", data_toggle = "tooltip", title = "Enter valid pkg value", @class = "form-control" })
                                        </div>
                                    </div>
                                </div>

                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.AirFare, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        @Html.TextBoxFor(model => model.AirFare, new { type = "number", @min = "0", @id = "airFare", data_toggle = "tooltip", title = "Enter valid airfare", @class = "form-control" })
                                    </div>
                                </div>

                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.Tax, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        @Html.TextBoxFor(model => model.Tax, new { type = "number", @min = "0", @id = "tax", data_toggle = "tooltip", title = "Enter valid tax", @class = "form-control" })
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.DateChangeFee, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        @Html.TextBoxFor(model => model.DateChangeFee, new { type = "number", @min = "0", @id = "dateChangeFee", data_toggle = "tooltip", title = "Enter valid date change fee value", @class = "form-control" })
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.CommissionToAgent, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        @Html.TextBoxFor(model => model.CommissionToAgent, new { type = "number", @min = "0", @id = "commissionToAgent", data_toggle = "tooltip", title = "Enter valid commission to agent value", @class = "form-control" })
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.VMPDNumber, new { @class = "label_class" })
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            @Html.TextBoxFor(model => model.VMPDNumber, new { type = "text", maxlength = "13", @id = "vmpdNumber", data_toggle = "tooltip", title = "Enter vmpd number", @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.EMDNumber, new { @class = "label_class", @style = "margin-top: -3px;" })
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            @Html.TextBoxFor(model => model.EMDNumber, new { type = "text", maxlength = "13", @id = "emdNumber", data_toggle = "tooltip", title = "Enter emd number", @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.RoomType, new { @class = "label_class", @style = "margin-top: -3px;" })
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            @Html.TextBox(item.RoomType, new { type = "text", @id = "roomType", @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <div class="col-sm-6">
                                        @Html.LabelFor(model => model.RoomCat, new { @class = "label_class", @style = "margin-top: -3px;" })
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            @Html.TextBox(item.RoomCategory, new { type = "text", @id = "roomCat", @class = "form-control" })
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
         </div>
  <div class="row">
            <p>
                <input type="button" class="btn btn-info btn-add" name="PaymentDetails" id="PaymentDetails_btn" value="Add" data-url="@Url.Action("PaymentDetails", "EVoucher")" />
            </p>
        </div>
        }

    </div>
</div>

当用户通过使用Ajax调用单击“添加”按钮时,我可以将值保存到数据库。但是如何根据相关迭代更改foreach循环值?

任何帮助都会非常感激。感谢

更新 - @Liam,我想根据foreach循环值更改@ item.RoomType,@ item.RoomCategory。但我不想一次显示所有输入。对于ex,如果我有20,我只想一次显示一个。不是全部20。

0 个答案:

没有答案
相关问题