插入外键记录asp.net mvc控制器

时间:2019-05-20 10:27:42

标签: c# asp.net-mvc-4

我想为产品销售添加产品详细信息,并且无法从控制器中引用销售ID,因此创建了如何使用外键从销售到销售详细信息引用销售ID

public ActionResult mainsales(int id = 0) 
{
    using(model db = new model()) 
    {
        dto1 = new Sale();
        int salesnumber = Convert.ToInt32(Request.Form["salesnumber"]);
        dto1.SalesNumber = salesnumber;
        db.Sales.Add(dto1);
        db.SaveChanges();
    }
    return View();
}

public ActionResult addsales(int id = 0) 
{
    long rfid = (long) Convert.ToDouble(Request.Form["rfid"]);

    Category_Item_File item;
    using(model db = new model()) 
    {

        Category_Item_File dto = db.Category_Item_File.FirstOrDefault(i => i.Item_RFID == rfid);
        SalesDetail saledet = new SalesDetail();

        saledet.Item_Name = dto.Item_Name;
        saledet.SalesPrice = dto.Item_Price;

        saledet.SalesId = dto1.Id;

        db.SalesDetails.Add(saledet);
        db.SaveChanges();

        if (dto == null) 
        {
        }
        item = new Category_Item_File(dto);

        return View("Till");
    }
}

0 个答案:

没有答案