public ActionResult Create([Bind(Include = "CostsDate, Price, Comment")] OtherCosts newOtherCost) { if (ModelState.IsValid) { newOtherCost.CreateDate = DateTime.Now; db.OtherCosts.Add(newOtherCost); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(newOtherCost)); }
public ActionResult Create([Bind(Include = "PriceOtherCosts, CommentOtherCosts")] AdmissionCreateViewModels model) { if (ModelState.IsValid) { if (Session["CreatedAdmissions"] != null) { int summCount = (Session["CreatedAdmissions"] as List <Admission>).Sum(c => c.Count); foreach (var newAdmission in (Session["CreatedAdmissions"] as List <Admission>)) { //создать поступление Admission admission = new Admission(); admission.AdmissionDate = newAdmission.AdmissionDate; admission.CreateDate = newAdmission.CreateDate; admission.ProviderId = newAdmission.ProviderId; admission.UserId = newAdmission.UserId; admission.TradePrice = newAdmission.TradePrice; admission.ProductId = newAdmission.ProductId; admission.Count = newAdmission.Count; //если есть прочие расходы вычислить доп. расходы if (model.PriceOtherCosts != null) { admission.AdditionalCosts = model.PriceOtherCosts / summCount; } db.Admissions.Add(admission); //изменить количество у товара Product product = db.Products.Where(i => i.Id == newAdmission.ProductId).FirstOrDefault(); if (product != null) { int newCount = product.Count + newAdmission.Count; product.Count = newCount; } else { return(HttpNotFound()); } } //создать прочие расходы if (model.PriceOtherCosts != null) { OtherCosts otherCosts = new OtherCosts(); otherCosts.CreateDate = DateTime.Now; otherCosts.CostsDate = (Session["CreatedAdmissions"] as List <Admission>).First().AdmissionDate; otherCosts.Price = (int)model.PriceOtherCosts; otherCosts.Comment = model.CommentOtherCosts != null ? model.CommentOtherCosts : null; otherCosts.Admission = true; db.OtherCosts.Add(otherCosts); } db.SaveChanges(); } else { return(RedirectToAction("Create")); } return(RedirectToAction("Index")); } return(RedirectToAction("Create")); }