Daniel
Daniel

Reputation: 311

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

I am trying to update data from two tables; products and inventory. The main key of the table products is cod_prod, which is the barcode of a product. This is the relationship with the products table and the other. 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. Only the cod_prod field on the inventory table is not updated and I don't know why.

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:

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:

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; }
}

Upvotes: 0

Views: 1536

Answers (2)

Jaggan_j
Jaggan_j

Reputation: 518

Suppose you have one to one relation between Products and Inventory tables, your models will look like this in 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:

[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.

Upvotes: 2

Daniel
Daniel

Reputation: 311

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. I had to do this way because the removal of the PK on the products table causes the null thing.

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");
        }
    }

Upvotes: 0

Related Questions