Я хочу залить модель из другой во время сохранения - PullRequest
0 голосов
/ 06 августа 2020

Спасибо за помощь

Я хочу сохранить данные, которые я отправляю с просмотра на две модели

подробнее: У меня есть Модель Production, Stock и storeInsert Я хочу отправить те же данные, которые я отправил в модели на складе в модели магазина, но другим способом я хочу получить StoreID в соответствии с идентификатором пользователя, чем получить ColorID из модели Fabri c, и получить ItemID из модели Production, чем вставляйте строку за строкой с указанием sizeID и количества из модели на складе.

производственная модель:

public class Production
{
    public int ID { get; set; }
    public int ItemID { get; set; }
    public int FabricID { get; set; }
    [Display(Name = "Quantity")]
    public decimal FabricQuantity { get; set; }
    [Display(Name = "Butten Holes")]
    public decimal ButtenHoles { get; set; }
    public decimal Cutting { get; set; }
    public decimal Workmenship { get; set; }
    [Display(Name = "Accessories Cost")]
    public decimal AccessoriesCost { get; set; }
    public virtual List<Stock> stock { get; set; }
    public virtual Item item { get; set; }
    public virtual Fabric fabric { get; set; }
    public virtual List<StoreInsert> storeInsert { get; set; }
}

Модель на складе:

public class Stock
{
    
    public int ID { get; set; }
    public int ProductionID { get; set; }
    public int SizeID { get; set; }
    public int Quantity { get; set; }
    
    public string ShelfNumber { get; set; }
    
    public virtual Production Production { get; set; }
    
    public virtual Size size { get; set; }
    public virtual List<StockRemain> stockRemain { get; set; }


}

Модель в магазине:

 public class Store
{
    public int ID { get; set; }
    public string Name { get; set; }
    [DefaultValue(false)]
    public bool Main { get; set; }
    
    public string Address { get; set; }
    public string Mobile { get; set; }
    public virtual List< ApplicationUser> user { get; set; }
    public virtual List<StoreInsert> storeInsert { get; set; }
}

Модель StoreInsert:

 public class StoreInsert
{
    public int ID { get; set; }
    public int ItemID { get; set; }
    public int StoreID { get; set; }
    public int SizeID { get; set; }
    public int ColorID { get; set; }
    public int Quantity { get; set; }
    [ForeignKey("ItemID")]
    public virtual Item  item { get; set; }
    [ForeignKey("StoreID")]
    public virtual Store store { get; set; }
    [ForeignKey("SizeID")]
    public virtual Size size { get; set; }
    [ForeignKey("ColorID")]
    public virtual Colors color { get; set; }
}

Это мой контроллер:

public JsonResult SaveProduction(Production production)
    {
        bool status = false;
     
        if (ModelState.IsValid)
        {
            string userID =  User.Identity.GetUserId();
            var storeID = from S in db.Users
                          where userID == S.Id
                          select S.StoreID;
            Production P = new Production { ItemID = production.ItemID, FabricID = production.FabricID, FabricQuantity = production.FabricQuantity, ButtenHoles = production.ButtenHoles, Cutting = production.Cutting,Workmenship = production.Workmenship,AccessoriesCost = production.AccessoriesCost };
            foreach (var i in production.stock)
            {
                P.stock.Add(i);
            }
            StoreInsert insertToStore = new StoreInsert();
            foreach (var item in production.stock)
            {
                insertToStore.StoreID = Convert.ToInt32(storeID);
                insertToStore.ColorID = production.fabric.ColorID.Value;
                insertToStore.SizeID = production.stock.Select(x=>x.SizeID).Single();
                insertToStore.ItemID = production.ItemID;
                insertToStore.Quantity = production.stock.Select(x => x.Quantity).Single();
                P.storeInsert.Add(insertToStore);
            }
            
            db.Productions.Add(P);
            db.SaveChanges();
            status = true;
        }
        return new JsonResult { Data = new { status = status } };


    }

Я получаю ошибку в foreach (var i в production.stock), это кажется пустым

я не знаю почему, и если вы знаете лучший способ из этого кода, пожалуйста, скажите мне

Большое спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...