I am inserting rows into table ,some time inserting null values from dropdownlist to table below is image.
Code
$(document).ready(function () {
$("#Search_Prdno").change(function () {
var prdno = $("#Search_Prdno").val();
if (prdno != "") {
GetBarcodeSB(prdno);
GetBarcodeweight(prdno);
}
else {
$("#codeitem").empty();
}
});
});
function GetBarcodeSB(prdno) {
$.ajax({
async: true,
type: 'GET',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
url: '/OrderPack/GetBarcodeSB',
data: { prdno: prdno },
success: function (data) {
$('#select2-3').empty();
$(data).each(function (index, item) {
$('#select2-3').append($('<option/>', { value: item.Value, text: item.Text }))
;
})
},
error: function () {
alert("There is some problem to get.");
$("#P_Prdno").empty();
$("#P_OrderNo").empty();
$("#I_Weigth").empty();
}
});
}
function GetBarcodeweight(prdno) {
$.ajax({
async: true,
type: 'GET',
dataType: 'JSON',
url: '/OrderPack/GetBarcodeweight',
data: { prdno: prdno },
success: function (data) {
console.log(data);
//populate the didden field
$("#Prdno").val(data.Prdno);
//populate the label
$("#Barcode_Bale").val(data.Prdno);
//populate the didden field
$("#OrderNo").val(data.OrderNo);
//populate the Orderno
$("#P_OrderNo").text(data.OrderNo);
//populate the didden field
$("#weight").val(data.weight);
//populate the label
$("#I_Weigth").text(data.weight);
addrow(prdno);
////console.log($("#select2-1").val());
//var orderNo1 = $('#P_OrderNo').html();
//var orderNo2 = $('#Orderno-label-5').html();
//if (orderNo1 == orderNo2) {
// alert('same order');
//}
//else {
// alert('Not same order');
//}
},
//error: function () {
// //alert("There is some problem to get.");
//}
});
}
function addrow() {
$('#btnInsert').add(function () {
var tbody = $('#DataInsert tbody');
var tr = $('<tr></tr>');
tr.append('<td>' + $('#Barcode_Bale').val() + '</td>');
tr.append('<td>' + $('#Bale_Qty').val() + '</td>');
tr.append('<td>' + $('#select2-3').text() + '</td>');
tr.append('<td>' + $('#select2-3').val() + '</td>');
tr.append('<td><input class="del" type="button" value="Delete" /></td>')
tbody.append(tr);
$("#Barcode_Bale").empty();
$("#select2-3").empty();
});
$('#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);
}
});
});
};
<div style="display: inline-block;">
<div>
@Html.LabelFor(model => model.Codeitem, "Select Item", htmlAttributes: new { @class = "control-label col-md-3" })
@Html.DropDownList("codeitemId", (SelectList)ViewBag.Codeitem, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-3" })
@Html.ValidationMessageFor(model => model.Codeitem, "", new { @class = "text-danger" })
</div>
</div>
<div style="display: inline-block;">
<div>
@Html.LabelFor(model => model.Bale_QTY, "Bale_QTY", htmlAttributes: new { @class = "control-label col-md-4" })
@Html.EditorFor(model => model.Bale_QTY, new { htmlAttributes = new { @class = "form-control col-md-10", @id = "Bale_Qty", @Value = "1" } })
@Html.ValidationMessageFor(model => model.Bale_QTY, "", new { @class = "text-danger" })
</div>
</div>
<div style="display: inline-block;">
<div>
@Html.LabelFor(model => model.Barcode_Bale, "BarCode", htmlAttributes: new { @class = "control-label col-md-4" })
@Html.EditorFor(model => model.Barcode_Bale, new { htmlAttributes = new { @class = "form-control col-md-10", @id = "Barcode_Bale" } })
@Html.ValidationMessageFor(model => model.Barcode_Bale, "", new { @class = "text-danger" })
</div>
</div>
<div style="display: inline-block;">
@Html.LabelFor(model => model.OrderNo, "OrderNo", htmlAttributes: new { @class = "control-label col-md-2", @id = "P_OrderNo" })
@Html.HiddenFor(Model => Model.OrderNo)
</div><div style="display: inline-block;">
@Html.LabelFor(model => model.weight, "Weight", htmlAttributes: new { @class = "control-label col-md-2", @id = "I_Weigth" })
@Html.HiddenFor(Model => Model.weight)
</div>
</div>
<div>
<input type="button" value="Insert" id="btnInsert" />
<br />
<br />
<table id="DataInsert">
<thead>
<tr>
<th>Barcode</th>
<th>QTY</th>
<th>Item</th>
<th>CodeiTem</th>
</tr>
</thead>
<tbody></tbody>
</table>
<br />
<input type="button" value="Save" id="btnSave" />
</div>
</div>