public ActionResult AddCollectible() { using (var db = new CollectibleContext()) { var model = new AddCollectible { Manufacturers = db.Manufacturers.OrderBy(x => x.Name).ToList() }; return(View(model)); } }
public ActionResult AddCollectible(AddCollectible model) { using (var db = new CollectibleContext()) { if (ModelState.IsValid) { int?pictureId = null; if (model.Photo != null && model.Photo.ContentLength > 0) { var photo = new CollectibleImage { FileName = System.IO.Path.GetFileName(model.Photo.FileName), ContentType = model.Photo.ContentType }; using (var reader = new System.IO.BinaryReader(model.Photo.InputStream)) { photo.Content = reader.ReadBytes(model.Photo.ContentLength); } db.Images.Add(photo); db.SaveChanges(); pictureId = photo.Id; } var collectible = new Collectible { DateLogged = DateTime.Now, DatePurchased = model.DatePurchased, Description = model.Description, EstimatedValue = model.EstimatedValue, PictureId = pictureId, ManufacturerId = model.ManufacturerId, ModelNumber = model.ModelNumber, ModelType = model.ModelType, PurchasePrice = model.PurchasePrice }; db.Collectibles.Add(collectible); db.SaveChanges(); return(RedirectToAction("Index")); } model.Manufacturers = db.Manufacturers.OrderBy(x => x.Name).ToList(); return(View(model)); } }