//BlogAccountViewModel viewModel = new BlogAccountViewModel(blogPost, applicationUser); //BlogAccountViewModel account = new AccountModel(); //BlogAccountViewModel viewModel = new BlogAccountViewModel(blog, account); public BlogAccountViewModel(BlogPost blogPost, ApplicationUser applicationUser) { // TODO: Complete member initialization this.blogPost = blogPost; this.applicationUser = applicationUser; }
public ActionResult Edit(BlogPost blogPost, HttpPostedFileBase image) { if (image != null && image.ContentLength > 0) { //Check the file name to make sure it's a image var ext = Path.GetExtension(image.FileName).ToLower(); if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".gif" && ext != ".bmp") ModelState.AddModelError("image", "Invalid Image Type."); } if (ModelState.IsValid) { //The Posts refers to the Model IdentityModels - public DbSet<BlogPost> Posts { get; set; } if(!db.Posts.Local.Any(p=>p.Id == blogPost.Id)) db.Posts.Attach(blogPost); if (image != null) { //relative server path var filePath = "/Uploads/"; //Path on physical drive on server var absPath = Server.MapPath("~" + filePath); //Media URL for relative path blogPost.MediaURL = filePath + image.FileName; //Save image image.SaveAs(Path.Combine(absPath, image.FileName)); db.Entry(blogPost).Property(p => p.MediaURL).IsModified = true; } blogPost.Updated = DateTimeOffset.Now; db.Entry(blogPost).Property(p => p.Updated).IsModified = true; db.Entry(blogPost).Property(p => p.Title).IsModified = true; db.Entry(blogPost).Property(p => p.Body).IsModified = true; if (db.Entry(blogPost).Property(p => p.Title).IsModified == true) { var Slug = StringUtilities.URLFriendly(blogPost.Title); if (String.IsNullOrWhiteSpace(Slug)) { ModelState.AddModelError("Title", "Invalid Title."); return View(blogPost); } } db.Entry(blogPost).Property(p => p.Slug).IsModified = true; db.Entry(blogPost).Property(p => p.IsPublished).IsModified = true; db.SaveChanges(); return RedirectToAction("Index"); } return View(blogPost); }
public ActionResult Create() { BlogPost BlogPost = new BlogPost(); BlogPost.IsPublished = true; return View(BlogPost); }