简体   繁体   English

如何从编辑视图 ASP.NET MVC 更新两个表?

[英]how update two tables from Edit View ASP.NET MVC?

I am trying to update data from two tables;我正在尝试更新两个表中的数据; products and inventory . productsinventory The main key of the table products is cod_prod , which is the barcode of a product.products的主键是cod_prod ,它是产品的条码。 This is the relationship with the products table and the other.这是与products表和其他的关系。 The update is carried out for all the fields, but in the database administrator, the cod_prod field in the inventory table is not updated, it only becomes null , in the products table the update is carried out, the reg_date field, which is a field in the inventory table is also updated.对所有字段都进行了更新,但是在数据库管理员中, inventory表中的cod_prod字段没有更新,它只是变为null ,在products表中进行了更新, reg_date字段,这是一个字段在inventory表中也更新。 Only the cod_prod field on the inventory table is not updated and I don't know why.只有inventory表上的cod_prod字段没有更新,我不知道为什么。

ViewModel:视图模型:

public class products
{

   [Display(Name = "Name")]
   public string name { get; set; }

   [Key]
   [Display(Name = "Product Code")]
   public string cod_prod { get; set; }

   [Display(Name = "Register Date")]
   [DataType(DataType.Date)]
   [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
   public DateTime? reg_date { get; set; }
}

Controller:控制器:

[HttpGet]
public ActionResult prodEdit(int id)
{
    using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
    {
        var u = dc.products.Where(a => a.id == id).FirstOrDefault();
        if (u != null)
        {
            var pm = new products
            {
                name = u.name,
                cod_prod = u.cod_prod,
                reg_date = u.reg_date
            };

            var b = dc.inventory.Where(x => x.cod_prod == pm.cod_prod).FirstOrDefault();

            u.cod_prod = b.cod_prod;

            return View(u);
        }

        return Content("Invalid Request");

    }
}

[HttpPost]
public ActionResult prodEdit(products prod)
{
    using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
    {

        var u = dc.products.Where(a => a.id == prod.id).FirstOrDefault();

        var b = dc.inventory.Where(x => x.cod_prod == prod.cod_prod).FirstOrDefault();

        inventory bod = new inventory()
        {
            cod_prod = prod.cod_prod,
            reg_date = prod.reg_date
        };

        
        dc.inventory.Remove(b);
        dc.inventory.Add(bod);
        dc.products.Remove(u);
        dc.products.Add(prod);
        dc.SaveChanges();

        return RedirectToAction("prodList", "products");

    }
}

Any suggestion is appreciated.任何建议表示赞赏。

UPDATE:更新:

Model for products : products型号:

public partial class products
{

   [Display(Name = "Name")]
   public string name { get; set; }

   [Key]
   [Display(Name = "Product Code")]
   public string cod_prod { get; set; }
}

Model for inventory : inventory模型:

public partial class inventory
{
   [Key]
   [Display(Name = "Product Code")]
   public string cod_prod { get; set; }

   [Display(Name = "Register Date")]
   [DataType(DataType.Date)]
   [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
   public DateTime? reg_date { get; set; }
}

Suppose you have one to one relation between Products and Inventory tables, your models will look like this in EF:假设您在 Products 和 Inventory 表之间有一对一的关系,您的模型在 EF 中将如下所示:

Products model产品型号

public class Products
{

   [Display(Name = "Name")]
   public string name { get; set; }

   [Key]
   [Display(Name = "Product Code")]
   public string cod_prod { get; set; }

   public virtual Inventory Inventory {get;set;}
}

Inventory model库存模型

public class Inventory
{
   [Key, ForeignKey("Products")]
   [Display(Name = "Product Code")]
   public string cod_prod { get; set; }

   [Display(Name = "Register Date")]
   [DataType(DataType.Date)]
   [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
   public DateTime? reg_date { get; set; }

   public virtual Products Products {get;set;}
}

Once relation is configured, you can simply do this in the POST method to update product and inventory:配置关系后,您可以简单地在 POST 方法中执行此操作以更新产品和库存:

[HttpPost]
public ActionResult prodEdit(Products prod)
{
    using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
    {

        var product = dc.products.Where(a => a.id == prod.id).FirstOrDefault();

        var inventory = product.Inventory;

        inventory.cod_prod = prod.cod_prod;
        inventory.reg_date = prod.reg_date;
        dc.SaveChanges();

        return RedirectToAction("prodList", "products");

    }
}

You can read more about how to configure EF relation here .您可以在此处阅读有关如何配置 EF 关系的更多信息。

If the same thing happens to someone, this is what I wrote to resolve it, the controller has two post methods, the first removes the fields that were changed, save data base and send the products and inventory objects to the second method, there, adds the new data of the models and save.如果同样的事情发生在某人身上,这就是我写的来解决它, controller有两个post方法,第一个删除更改的字段,保存数据库并将productsinventory对象发送到第二个方法,在那里,添加模型的新数据并保存。 I had to do this way because the removal of the PK on the products table causes the null thing.我不得不这样做,因为在products表上删除 PK 会导致null事情。

Controller:控制器:

    [HttpPost]
    public ActionResult prodEdit(products prod)
    {
        using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
        {
            var u = dc.products.Where(a => a.id == prod.id).FirstOrDefault();

            if(u != null)
            {
                var pm = new products
                {
                    prod_name = prod.prod_name,
                    cod_prod = prod.cod_prod,
                    fecha_ingreso = prod.fecha_ingreso
                };

                var b = dc.bodega.Where(x => x.cod_prod == u.cod_prod).FirstOrDefault();
                
                if (b != null)
                {

                    inventory inv = new inventory()
                    {
                        reg_date = pm.fecha_ingreso,
                        cod_prod = pm.codigo_prod
                    };



                    if (inv.cod_prod != null)
                    {
                        dc.inventory.Remove(b);
                        dc.products.Remove(u);

                        dc.SaveChanges();

                        prodEdit2(prod, bod);
                    }
                }
            }
            return RedirectToAction("prodList", "products");
        }
    }

    [HttpPost]
    public ActionResult prodEdit2(products p, inventory i)
    {
        using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
        {
            dc.products.Add(p);
            dc.inventory.Add(i);

            dc.SaveChanges();

            return RedirectToAction("prodList", "products");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM