public ActionResult Create(PlanViewModel plan) { if(ModelState.IsValid) { var newPlan = new Plan { Created = DateTimeOffset.UtcNow, Updated = DateTimeOffset.UtcNow, Name = plan.Name.Trim(), Area = plan.Area, Price = plan.Price }; context.Plans.Add(newPlan); context.SaveChanges(); plan.ID = newPlan.ID; if(plan.PreviewImageFile != null || plan.ModelFilePath != null) { string root = ConfigurationManager.AppSettings["UserDataRoot"]; root = Path.Combine(root, "plans", newPlan.ID.ToString()); IStorage storage = this.GetStorage(Server.MapPath(root)); storage.EnsureRootExist(); // Preview string fileName = newPlan.Updated.UtcTicks.ToString(); if(plan.PreviewImageFile != null && plan.PreviewImageFile.ContentLength > 0 || plan.ModelFile != null && plan.ModelFile.ContentLength > 0) { string ext = plan.PreviewImageFile.InputStream.GetFileExtension(); ext = ext == null ? null : ext.ToLower(); storage.Save(plan.PreviewImageFile.InputStream, fileName + ext); newPlan.PreviewImageFilePath = Path.Combine(root, fileName + ext); } if(plan.ModelFile != null && plan.ModelFile.ContentLength > 0) { string ext = plan.ModelFile.InputStream.GetFileExtension(); ext = ext == null ? null : ext.ToLower(); storage.Save(plan.ModelFile.InputStream, fileName + ext); newPlan.ModelFilePath = Path.Combine(root, fileName + ext); } context.Entry<Plan>(newPlan).State = EntityState.Modified; context.SaveChanges(); } return RedirectToAction("Edit", new { ID = newPlan.ID }); } return View(plan); }
public ActionResult Edit(PlanViewModel plan) { using(var dtx = new HeimContext()) { var planModel = dtx.Plans.Include("Floors").Single( s => s.ID == plan.ID ); //plan.Attributes = planModel.Attributes.ToList(); if(ModelState.IsValid) { planModel.Updated = DateTimeOffset.UtcNow; planModel.Name = plan.Name.Trim(); planModel.Area = plan.Area; planModel.Price = plan.Price; IStorage storage = null; string root = null; // prepare storage if(plan.PreviewImageFile != null && plan.PreviewImageFile.ContentLength > 0 || plan.ModelFile != null && plan.ModelFile.ContentLength > 0) { string fileName = planModel.Updated.UtcTicks.ToString(); root = ConfigurationManager.AppSettings["UserDataRoot"]; root = Path.Combine(root, "plans", plan.ID.ToString()); storage = this.GetStorage(Server.MapPath(root)); storage.EnsureRootExist(); if(plan.PreviewImageFile != null && plan.PreviewImageFile.ContentLength > 0) { string ext = plan.PreviewImageFile.InputStream.GetFileExtension(); ext = ext == null ? null : ext.ToLower(); storage.Save(plan.PreviewImageFile.InputStream, fileName + ext); planModel.PreviewImageFilePath = Path.Combine(root, fileName + ext); } if(plan.ModelFile != null && plan.ModelFile.ContentLength > 0) { string ext = plan.ModelFile.InputStream.GetFileExtension(); ext = ext == null ? null : ext.ToLower(); storage.Save(plan.ModelFile.InputStream, fileName + ext); planModel.ModelFilePath = Path.Combine(root, fileName + ext); } } context.Entry<Plan>(planModel).State = EntityState.Modified; context.SaveChanges(); if(plan.PreviewImageFile == null) { return RedirectToAction("Index"); } } return RedirectToAction("Edit", new { id = plan.ID }); } }