Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre>[HttpPost]
        public JsonResult SaveClientOrder(Sale sale, List<SalesDetail> salesDetails)
        {
           
 NeamahBranch.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage();
            InvoiceBranchs_DBEntities db = new InvoiceBranchs_DBEntities();
            retMessage.IsSuccess = true;

            foreach (var item in salesDetails)
            {
                sale.SalesDetails.Add(new SalesDetail { ProductId = item.ProductId, UnitPrice = item.UnitPrice, Quantity = item.Quantity, LineTotal = item.LineTotal });
            }

            db.Sales.Add(sale);
            retMessage.Messagae = "Save Success!";
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                retMessage.IsSuccess = false;
            }
            return Json(retMessage, JsonRequestBehavior.AllowGet);

        }


What I have tried:

I have relationship between Product table and Sales Detail table if I add any product in product table appear in Sales Detail table by Id not Name Product
Posted
Updated 5-Sep-21 22:42pm

1 solution

That's how foreign-key database relationships work. The sales detail table should not contain details from the products table; it should only contain the key of the related record in the products table.

In SQL, you join the two tables together to see the related details. In Entity Framework, you use the navigation properties to view the related entity.
SQL
SELECT
    D.ProductId,
    P.Name As ProductName,
    D.UnitPrice,
    D.Quantity,
    D.LineTotal
FROM
    dbo.SalesDetails As D
    INNER JOIN dbo.Products As P
    ON P.Id = D.ProductId
WHERE
    D.SaleId = 1
;
C#
List<SaleDetail> sale = context.SaleDetails
    .Include(d => d.Product)
    .Where(d => d.SaleId == 1)
    .ToList();
 
Share this answer
 
Comments
Member 15345871 10-Sep-21 17:17pm    
First of all thanks for the help, where can I write this code

List<saledetail> sale = context.SaleDetails
.Include(d => d.Product)
.Where(d => d.SaleId == 1)
.ToList();

my Code :-

[HttpPost]
        public JsonResult SaveClientOrder(Sale sale, List<salesdetail> salesDetails)
        {
           
 NeamahBranch.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage();
            InvoiceBranchs_DBEntities db = new InvoiceBranchs_DBEntities();
            retMessage.IsSuccess = true;

            foreach (var item in salesDetails)
            {
                sale.SalesDetails.Add(new SalesDetail { ProductId = item.ProductId, UnitPrice = item.UnitPrice, Quantity = item.Quantity, LineTotal = item.LineTotal });
            }

            db.Sales.Add(sale);
            retMessage.Messagae = "Save Success!";
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                retMessage.IsSuccess = false;
            }
            return Json(retMessage, JsonRequestBehavior.AllowGet);

        }
Richard Deeming 13-Sep-21 5:14am    
You'd use that code when you want to return the details of a single sale, not when you're saving a sale.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900