public ActionResult Create(CoursePostViewModel course) { if (ModelState.IsValid) { //saving image var image = new WebImage(course.File.InputStream); var imageName = Guid.NewGuid().ToString() + "." + image.ImageFormat; var path = Path.Combine(Server.MapPath("~/Content/Images"), WebSecurity.CurrentUserName, "Courses"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, imageName); image.Save(path); //saving course var dbCourse = ViewModelIntoEntity(course); dbCourse.CreatedDate = DateTime.Now; dbCourse.LastModifiedDate = DateTime.Now; dbCourse.Published = false; dbCourse.ImageName = imageName; dbCourse.ImageUrl = Path.Combine("~/Content/Images", WebSecurity.CurrentUserName, "Courses", imageName); db.Courses.Add(dbCourse); db.SaveChanges(); return RedirectToAction("Courses", "Dashboard"); } ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", course.CategoryId); ViewBag.InstructorId = WebSecurity.CurrentUserId; return View(course); }
private Course ViewModelIntoEntity(CoursePostViewModel viewModel) { return new Course() { Name = viewModel.Name, Description = viewModel.Description, CategoryId = viewModel.CategoryId, InstructorId = viewModel.InstructorId }; }