Each row in html inserting two times in Database

Analyst_SQL 3,576 Reputation points
2023-03-22T06:40:25.18+00:00

i am inserting row from html to database,then each row from html table inserting into database two times,

    $('#btnSave').click(function () {
            var smallbales = new Array();
            $("#DataInsert tbody tr").each(function () {
                var row = $(this);
                var smallbale = {};
                smallbale.Barcode_Bale = row.find("TD").eq(0).html();
                smallbale.Bale_Qty = row.find("TD").eq(1).html();
                smallbale.select2 = row.find("TD").eq(2).html();
                smallbale.select2 = row.find("TD").eq(3).html();

                smallbales.push(smallbale);
            });
            $.ajax({
                type: "POST",
                url: '/OrderPack/InsertPack',
                data: JSON.stringify(smallbales),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert("Record(s) inserted.");
                },
                error: function (r) {
                    alert(r.responseText);
                }
            });

        });

public ActionResult InsertPack(List<OrderVm> smallbales)
        {
            if (smallbales != null)
            {
                foreach (OrderVm smallbale in smallbales)
                {
                    var newbigbale = new tbl_PckDetail();
                    newbigbale.Prdno = smallbale.Barcode_Bale;
                    newbigbale.QTY = smallbale.Bale_QTY;
                   
                    DB.tbl_PckDetail.Add(newbigbale);
                    DB.SaveChanges();
                    
                }
            }

            return View("Index");
        }

public class OrderVm
    {

        [Display(Name = "Select Order")]
        public int? OrderNo { get; set; }
        public int? SOrderNo { get; set; }
        public int? Codeitem { get; set; }
        [Display(Name = "Input Barcode")]
        public int? Prdno { get; set; }
         public int? Barcode_Bale { get; set; }
        public int? orderqty { get; set; }
        public int? prdqty { get; set; }
        public int? packQty { get; set; }

        public int? PID { get; set; }
        public int? Bale_QTY { get; set; }

              public int? weight { get; set; }

        public Probale smallbale { get; set; }
        public ItemMasterFile Itemasterfile { get; set; }
    }
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author

Lan Huang-MSFT 30,221 Reputation points Microsoft External Staff
2023-03-22T09:29:19.36+00:00

Hi @Analyst_SQL ,

The DataInsert table should contain the <td> itself, then add the <td> when adding rows. So row.find("TD").eq(0).html() gets two <td> s.

This might be the reason for inserting twice.

You can remove the <td> in the DataInsert table itself or change it to a <th>.

If it still doesn't work, I suggest you provide the code of view and tbl_PckDetail()

so that we can reproduce your problem and help you solve it better.

Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.