hi Every One nice to meet you Iam Happy for Register in this Site i have big problem for two week I Can not find solution for this error i work on project mvc core and make OrderMasterDetails and i want to save in database
But something wear is happining MasterDetails is Save well in dataBase but orderDetails I can not save in database Also i turn on break point but I can not See any Error .
Notice: i use Ajax to send data and ajax work good without Error But I think Something wrong in my Controller I can not Know What is it???
and here my View And Controller and my DB
Controller
[HttpPost]
public IEnumerable<OrderMasterDetails> addorder([FromBody]OrderMasterDetails ordermasterdetail)
{
OrderMaster orderMaster = new OrderMaster();
orderMaster.Order_Code = ordermasterdetail.Order_Code;
orderMaster.Hours = DateTime.Now.Hour.ToString();
int discount = 0;
int.TryParse(ordermasterdetail.discount, out discount);
orderMaster.discount = discount;
orderMaster.DateAdded = DateTime.Now;
orderMaster.BranchID = int.Parse(ordermasterdetail.BranchID);
orderMaster.Order_Kind_ID = int.Parse(ordermasterdetail.Order_Kind_ID);
int tax = 0;
int.TryParse(ordermasterdetail.tax, out tax);
orderMaster.tax = tax;
_Context.orderMasters.Add(orderMaster);
_Context.SaveChanges();
var dborder_Master = _Context.orderMasters.SingleOrDefault(c => c.Order_ID == orderMaster.Order_ID);
dborder_Master.Order_Code = ordermasterdetail.Order_Code + dborder_Master.Order_ID.ToString();
foreach (var item in ordermasterdetail.lstProducts)
{
OrderDetails orderDetails = new OrderDetails();
orderDetails.Order_ID = orderMaster.Order_ID;
orderDetails.Product_ID = int.Parse(item.Product_ID);
orderDetails.Product_Price = float.Parse(item.Product_Price);
orderDetails.Product_Quantity = int.Parse(item.Product_Quantity);
orderDetails.Unit_ID = int.Parse(item.Unit_ID);
_Context.orderDetails.Add(orderDetails);
_Context.SaveChanges();
}
return null;
}
View
@model OrderMasterDetails
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_layout.cshtml";
}
<h1>Create Order</h1>
<meta http-equiv="refresh" content="0; url=https://www.enable-javascript.com" />
</noscript>*@
<form method="post">
<table class="table table-bordered">
<tr>
<td>
<label class="control-label">Code</label>
</td>
<td>
<input value="@ViewBag.Order_Code" id="Order_Code" class="form-control" />
</td>
</tr>
<tr>
<td>
<label class="control-label">Order kind</label>
</td>
<td>
<select disabled="disabled" id="Order_Kind_ID" class="form-control" asp-items="ViewBag.Order_Kind_ID"></select>
</td>
</tr>
<tr>
<td>
<label class="control-label">Branch</label>
</td>
<td>
<select id="BranchID" class="form-control" asp-items="ViewBag.Branch_ID"></select>
</td>
</tr>
<tr>
<td>
<label class="control-label">tax</label>
</td>
<td>
<input id="tax" class="form-control" />
</td>
</tr>
<tr>
<td>
<label class="control-label">discount</label>
</td>
<td>
<input id="discount" class="form-control" />
</td>
</tr>
</table>
<table class="table table-borderless">
<tr>
<th>
product
</th>
<th>
unit
</th>
<th>
quantity
</th>
<th>
price
</th>
<th>
</th>
</tr>
<tr id="trrows">
<td>
<select id="Product_ID" class="Product_ID form-control" asp-items="ViewBag.Product_ID"></select>
</td>
<td>
<select id="Unit_ID" class="Unit_ID form-control" asp-items="ViewBag.Unit_ID"></select>
</td>
<td>
<input id="Product_Quantity" class="Product_Quantity form-control" />
</td>
<td>
<input id="Product_Price" class="Product_Price form-control" />
</td>
<td>
<button type="submit" id="addtocart" name="addtocart" class="btn btn-primary">add</button>
</td>
</tr>
</table>
</form>
<table id="tblrows" class="table table-hover">
</table>
<hr />
<button asp-action="addorder" type="submit" id="save" name="save" class="btn btn-primary">Save All</button>
@section Scripts {
<script src="~/lib/jquery/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#addtocart").click(function () {
var product_id = document.getElementById("Product_ID").value;
var trrow = $("#trrows").clone().removeAttr("id");
$('.Product_ID', trrow).val(product_id);
$("#addtocart", trrow).addClass('remove').html('Remove').removeClass('btn-primary').addClass('btn-danger');
$("#tblrows").append(trrow[0]);
});
$("#tblrows").on("click", ".remove", function () {
$(this).parents("tr").remove();
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#save").click(function () {
var lstitems = [];
jQuery("#tblrows tr").each(function () {
var item = {
"Product_ID": $('.Product_ID').val(),
"Product_Price": $('.Product_Price').val(),
"Product_Quantity": $('.Product_Quantity').val(),
"Unit_ID": $('.Unit_ID').val()
}
lstitems.push(item);
});
var order_code = $('#Order_Code').val();
var branch_id = $('#BranchID').val();
var tax = $('#tax').val();
var discount = $('#discount').val();
var order_kind_id = '1';
$.ajax({
url: '/OrderMasterDetails/addorder',
type: 'POST',
// data: formData,
dataType: 'json',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
data: JSON.stringify({
"Order_Code": order_code, "BranchID": branch_id, "tax": tax, "discount": discount, "Order_Kind_ID": order_kind_id, "lstProducts": lstitems
}),
success: function (data) {
// your code in success
alert('saved');
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
});
});
</script>
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
Model
public class OrderMasterDetails
{
public string Order_Code { get; set; }
public string BranchID { get; set; }
public string tax { get; set; }
public string discount { get; set; }
public string Order_Kind_ID { get; set; }
public List<ListProducts> lstProducts { get; set; }
}
public class ListProducts
{
public string Product_ID { get; set; }
public string Unit_ID { get; set; }
public string Product_Quantity { get; set; }
public string Product_Price { get; set; }
}
i saved Master in db but I can not Save OrderDetails
Please i want any help and At The End Really thanks For EveryOne