public ActionResult Create(TemplateViewModel templateViewModel) { var files = HttpContext.Request.Form.Files; //make sur they filled it out correctly if (this.ModelState.IsValid) { var template = new Flow { IsATemplate = true, Description = templateViewModel.description, name = templateViewModel.name, inputSurvey = new Survey { fields = templateViewModel.fields }, criteria = templateViewModel.criteria, forms = new List <Form>() }; //for each file that was uploaded, add a form to the template foreach (var file in templateViewModel.files) { template.forms.Add(new Form { assignments = new List <Assignment>(), fileName = Path.GetFileName(file.FileName), kind = "pdf", name = Path.GetFileNameWithoutExtension(file.FileName) }); } Flow newTemplate = database.StartNewTemplate(template); newTemplate = database.SaveNewTemplate(template); //for each file that was uploaded, copy the file to where it belongs foreach (var file in templateViewModel.files) { using (var readStream = file.OpenReadStream()) { using (var ms = new MemoryStream()) { readStream.CopyTo(ms); var bytes = ms.ToArray(); fileHandler.SaveForm(bytes, Path.GetFileNameWithoutExtension(file.FileName), newTemplate.Id, "pdf"); } } } if (templateViewModel.image != null) { using (var readStream = templateViewModel.image.OpenReadStream()) { using (var ms = new MemoryStream()) { readStream.CopyTo(ms); var bytes = ms.ToArray(); template.ThumbnailImage = fileHandler.SaveTemplateImage(bytes, Path.GetFileNameWithoutExtension(templateViewModel.image.FileName), template.Id); } } } else { Random rnd = new Random(); int pic = rnd.Next(1, 11); var imgFolderPath = fileHandler.GetImagesPath(); string imagePath = Path.Combine(imgFolderPath, (pic.ToString() + ".jpg")); template.ThumbnailImage = imagePath; var templateImage = Path.Combine(imgFolderPath, (template.Id.ToString() + ".jpg")); fileHandler.ClonePlaceHolder(imagePath, templateImage); } return(RedirectToAction("Fields", "Template", new { id = newTemplate.Id })); } return(View(templateViewModel)); }