如何使用JQuery在模式中编辑表

问题描述

NET MVC并在更新表时遇到问题。让我向您解释我的关注。我创建了一个包含四个产品项的采购订单,并将其保存在数据库中。现在,我想编辑该特定采购订单中的某些项目,但是我无法执行此操作。我从最近8个小时开始面临这个问题。基本上,整个概念取决于JQuery。因此,我请您帮助我,对此我将非常感谢。

<form name="EditpurchaseForm" id="EditpurchaseForm">
@Html.AntiForgeryToken()

<div class="modal-header">
    <h5 class="modal-title" id="modal-title">Update Purchase Order</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>


    <h5 style="color:blueviolet"> Order Details</h5>
    <hr />
    <div class="container">
        <div class="col">
            <div class="row-md-6">
                <div id="ui">
                    <div class="form-group">
                        <div class="row">
                            <div class="col-md-6">
                                @Html.LabelFor(x => x.ProductName)
                                @Html.DropDownListFor(x => x.Stock_ID,new SelectList(ViewBag.stock,"Stock_ID","Stock_Name"),"--Select Product Name--",new { id = "StockID",@class = "form-control" })
                            </div>

                            <div class="col-md-6">
                                @Html.LabelFor(x => x.Quantity)
                                @Html.TextBoxFor(x => x.Quantity,new { id = "Quantity",@class = "form-control" })
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                @Html.LabelFor(x => x.Price)
                                @Html.TextBoxFor(x => x.Price,new { id = "Price",@class = "form-control" })
                            </div>

                        </div>
                        <div class="col-md-12">
                            <a id="addToList" class="btn btn-primary" style="margin-top:-38px; float:right; color:white; font-weight:400;">Add To List</a>
                        </div>

                    </div>

                    <table id="detailsTable" class="table">
                        <thead>
                            <tr>
                                <th style="width:20%">Product Name</th>
                                <th style="width:16%">Quantity</th>
                                <th style="width:16%">Price</th>
                                <th style="width:16%">Amount</th>
                                <th style="width:20%"></th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.OrderDetails)
                            {
                                @Html.HiddenFor(x => @item.OrderID,new { id = "OrderID" })
                                <tr>
                                    <td>@item.ProductName</td>
                                    <td><span data-line_qty="@item.Quantity" data-itemId="0" href="#" class="qtyItem">@item.Quantity</span></td>
                                    <td>@item.Price</td>
                                    <td>@item.Amount</td>
                                    <td><a data-line_total="@item.Price * @item.Quantity" data-itemId="0" href="#" class="deleteItem">Remove</a></td>
                                </tr>
                            }
                        </tbody>
                    </table>
                    <div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-6">
                                    <strong>Total Quantity:</strong>

                                    @Html.TextBoxFor(x => x.TotalQuantity,new { id = "totalquantity",@class = "form-control totalquantity",@readonly = "readonly",@style = "width:auto" })
                                </div>
                                <div class="col-md-6">
                                    <strong>Total Bill:</strong>

                                    @Html.TextBoxFor(x => x.TotalAmount,new { id = "totalamount",@class = "form-control totalamount",@style = "width:auto" })
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-sm" data-dismiss="modal">Cancel</button>
        <button type="button" id="saveOrder" onclick="Update()" class="btn btn-sm btn-success pull-right">Save</button>
    </div>

</div>
<script>
$("#addToList").click(function (e) {
    e.preventDefault();
    debugger;
    if ($.trim($("#StockID").val()) == "" || $.trim($("#Quantity").val()) == "" || $.trim($("#Price").val()) == "")
        return;
    var orderid = $("#OrderID").val();
    var productName = $("#StockID option:selected").text();
    var pid = $("#StockID option:selected").val();
    let price = $("#Price").val();
    let quantity = $("#Quantity").val();

    let detailsTableBody = $("#detailsTable tbody");
    var productItem = `<tr>
                                <td pid=${pid}>
                                    ${productName}
                                </td>
                                <td><span data-line_qty="${quantity}" data-itemId="0" href="#" class="qtyItem" >${quantity}</span></td>

                                <td>${price}</td>
                                <td>${(parseFloat(price) * parseInt(quantity))}</td>
                               <td><a data-line_total="${(parseFloat(price) * parseInt(quantity,10))}" data-itemId="0" href="#" class="deleteItem">Remove</a></td>
                            </tr>`;

    detailsTableBody.append(productItem);
    calc_total();
    clearItem();
});

function Update() {
        var modal = $("#EditpurchaseModal");
        var form = $('form[name="EditpurchaseForm"]');

        let tableData = $('#detailsTable > tbody > tr');
        console.log(tableData);

        var tableDto = [];

        $.each(tableData,function (rindex,row) {
            console.log(row);
            let tmp = {};
            tmp.pid = $(row).find('td:nth-child(1)').attr('pid');
            tmp.productName = $(row).find('td:nth-child(1)').text().trim();
            tmp.qty = $(row).find('td:nth-child(2)').text();
            tmp.price = $(row).find('td:nth-child(3)').text();
            tmp.amount = $(row).find('td:nth-child(4)').text();
            tableDto.push(tmp);
            tmp = {};
        });
        
        form.validate();
        if (!form.valid()) {
            return;
        }
        else {
            $.post('/Home/UpdatePurchase',{
                purchase: purchaseDt,tableListDto: tableDto
            }).done(function (result) {
                modal.modal('hide');
                dataTable.ajax.reload();
                $.notify("Saved Successfully",{
                    className: "success",globalPosition: 'top - center'
                });
            }).fail(function () {
                window.location.href = "";
            });
        }
        }
</script>

在这种情况下,我没有获得OrderID。请帮我。 这是编辑页面图像。我正在获取表中的所有项目详细信息。如果我进行了任何更改,它将不会保存在我的数据库中。

This is Edit Page Image. I am getting all the item details in the table. If i made any change it will not save in my database.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)