public IActionResult Edit(Guid id) { var notes = Note.GetAllByUserId(this.GetCurrentUser().Id); var model = new EditNotesViewModel(); model.Notes = notes; model.NoteForEdit = notes.Single(x => x.Id == id); return(View("Edit", model)); }
public ActionResult EditNotes(int id) { // get current user Users user = context.Users.Where(x => x.EmailID == User.Identity.Name).FirstOrDefault(); // get sellernotes table details SellerNotes note = context.SellerNotes.Where(x => x.ID == id && x.IsActive == true && x.SellerID == user.ID).FirstOrDefault(); // get noteattachement table details SellerNotesAttachements attachement = context.SellerNotesAttachements.Where(x => x.NoteID == id).FirstOrDefault(); if (note != null) { // create object of edit note viewmodel EditNotesViewModel Model = new EditNotesViewModel { ID = note.ID, NoteID = note.ID, Title = note.Title, Category = note.Category, Picture = note.DisplayPicture, Note = attachement.FilePath, NumberofPages = note.NumberofPages, Description = note.Description, NoteType = note.NoteType, UniversityName = note.UniversityName, Course = note.Course, CourseCode = note.CourseCode, Country = note.Country, Professor = note.Professor, IsPaid = note.IsPaid, SellingPrice = note.SellingPrice, Preview = note.NotesPreview, NoteCategoryList = context.NoteCategories.Where(x => x.IsActive == true).ToList(), NoteTypeList = context.NoteTypes.Where(x => x.IsActive == true).ToList(), CountryList = context.Countries.Where(x => x.IsActive == true).ToList() }; // return viewmodel to edit notes page return(View(Model)); } else { // if note not found return(HttpNotFound()); } }
public ActionResult EditNotes(int id, EditNotesViewModel notes) { // check if model state is valid or not if (ModelState.IsValid) { // get logged in user var user = Context.Registration.Where(x => x.Email == User.Identity.Name).FirstOrDefault(); // get note var sellernotes = Context.SellerNotes.Where(x => x.ID == id && x.IsActive == true && x.SellerID == user.UserId).FirstOrDefault(); // if sellernote null if (sellernotes == null) { return(HttpNotFound()); } // check if note is paid or preview is not null if (notes.IsPaid == true && notes.Preview == null && sellernotes.NotesPreview == null) { //ModelState.AddModelError("NotesPreview", "This field is required if selling type is paid"); @ViewBag.Notespreview = "This field is required if selling type is paid"; return(View(notes)); } // get note attachement var notesattachement = Context.SellerNotesAttachments.Where(x => x.NoteID == notes.NoteID && x.IsActive == true).ToList(); // attache note object and update Context.SellerNotes.Attach(sellernotes); sellernotes.Title = notes.Title.Trim(); sellernotes.Category = notes.Category; sellernotes.NoteType = notes.NoteType; sellernotes.NumberofPages = notes.NumberofPages; sellernotes.Description = notes.Description.Trim(); sellernotes.Country = notes.Country; sellernotes.UniversityName = notes.UniversityName.Trim(); sellernotes.Course = notes.Course.Trim(); sellernotes.CourseCode = notes.CourseCode.Trim(); sellernotes.Professor = notes.Professor.Trim(); if (notes.IsPaid == true) { sellernotes.IsPaid = true; sellernotes.SellingPrice = notes.SellingPrice; } else { sellernotes.IsPaid = false; sellernotes.SellingPrice = 0; } Context.SaveChanges(); // if display picture is not null if (notes.DisplayPicture != null) { // if note object has already previously uploaded picture then delete it if (sellernotes.DisplayPicture != null) { string path = Server.MapPath(sellernotes.DisplayPicture); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); } } // save updated profile picture in directory and save directory path in database string displaypicturefilename = System.IO.Path.GetFileName(notes.DisplayPicture.FileName); string displaypicturepath = "~/Members/" + user.UserId + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); sellernotes.DisplayPicture = displaypicturepath + displaypicturefilename; notes.DisplayPicture.SaveAs(displaypicturefilepath); } // if note preview is not null if (notes.NotesPreview != null) { // if note object has already previously uploaded note preview then delete it if (sellernotes.NotesPreview != null) { string path = Server.MapPath(sellernotes.NotesPreview); FileInfo file = new FileInfo(path); if (file.Exists) { file.Delete(); } } // save updated note preview in directory and save directory path in database string notespreviewfilename = System.IO.Path.GetFileName(notes.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.UserId + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); sellernotes.NotesPreview = notespreviewpath + notespreviewfilename; notes.NotesPreview.SaveAs(notespreviewfilepath); } // check if user upload notes or not if (notes.UploadNotes[0] != null) { // if user upload notes then delete directory that have previously uploaded notes string path = Server.MapPath(notesattachement[0].FilePath); DirectoryInfo dir = new DirectoryInfo(path); EmptyFolder(dir); // remove previously uploaded attachement from database foreach (var item in notesattachement) { SellerNotesAttachments attachement = Context.SellerNotesAttachments.Where(x => x.ID == item.ID).FirstOrDefault(); Context.SellerNotesAttachments.Remove(attachement); } // add newly uploaded attachement in database and save it in database foreach (HttpPostedFileBase file in notes.UploadNotes) { // check if file is null or not if (file != null) { // save file in directory string notesattachementfilename = System.IO.Path.GetFileName(file.FileName); string notesattachementpath = "~/Members/" + user.UserId + "/" + sellernotes.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); // create object of sellernotesattachement SellerNotesAttachments notesattachements = new SellerNotesAttachments { NoteID = sellernotes.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, IsActive = true }; // save seller notes attachement Context.SellerNotesAttachments.Add(notesattachements); Context.SaveChanges(); } } } return(RedirectToAction("Dashboard", "Dashboard")); } else { return(RedirectToAction("EditNotes", new { id = notes.ID })); } }