public ActionResult AddNotes() { AddNotesViewModel Model = new AddNotesViewModel { 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(View(Model)); }
public ActionResult AddNotes() { // create add note viewmodel and set values in dropdown list AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(), NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(), CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList() }; return(View(viewModel)); }
//get data for dropdown in add notes public AddNotesViewModel GetData() { var category = db.NoteCategories.Where(x => x.IsActive == true).ToList(); var type = db.NoteTypes.Where(x => x.IsActive == true).ToList(); var country = db.Countries.Where(x => x.IsActive == true).ToList(); AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = category, NoteTypeList = type, CountryList = country }; return(viewModel); }
public ActionResult AddNotes() { ViewBag.navclass = "white-nav"; ViewBag.SellNotes = "active"; //Fetch data from database var category = db.NoteCategories.Where(x => x.IsActive == true).ToList(); var type = db.NoteTypes.Where(x => x.IsActive == true).ToList(); var country = db.Countries.Where(x => x.IsActive == true).ToList(); AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = category, NoteTypeList = type, CountryList = country }; return(View(viewModel)); }
public ActionResult AddNotes(AddNotesViewModel addnotesviewmodel) { // check if upload note is null or not if (addnotesviewmodel.UploadNotes[0] == null) { ModelState.AddModelError("UploadNotes", "This field is required"); addnotesviewmodel.NoteCategoryList = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } // check and raise error for note preview is null for paid notes if (addnotesviewmodel.IsPaid == true && addnotesviewmodel.NotesPreview == null) { ModelState.AddModelError("NotesPreview", "This field is required if selling type is paid"); addnotesviewmodel.NoteCategoryList = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } foreach (HttpPostedFileBase file in addnotesviewmodel.UploadNotes) { if (!System.IO.Path.GetExtension(file.FileName).Equals(".pdf")) { ModelState.AddModelError("UploadNotes", "Only PDF Format is allowed"); addnotesviewmodel.NoteCategoryList = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(); addnotesviewmodel.CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList(); return(View(addnotesviewmodel)); } } // check model state if (ModelState.IsValid) { // create seller note object SellerNote sellernotes = new SellerNote(); User user = _dbcontext.Users.FirstOrDefault(x => x.EmailID == User.Identity.Name); sellernotes.SellerID = user.UserID; sellernotes.Title = addnotesviewmodel.Title.Trim(); sellernotes.Status = _dbcontext.ReferenceDatas.Where(x => x.Value.ToLower() == "draft").Select(x => x.ID).FirstOrDefault(); sellernotes.Category = addnotesviewmodel.Category; sellernotes.NoteType = addnotesviewmodel.NoteType; sellernotes.NumberOfPages = addnotesviewmodel.NumberofPages; sellernotes.Description = addnotesviewmodel.Description.Trim(); sellernotes.UniversityName = addnotesviewmodel.UniversityName.Trim(); sellernotes.Country = addnotesviewmodel.Country; sellernotes.Course = addnotesviewmodel.Course.Trim(); sellernotes.CourseCode = addnotesviewmodel.CourseCode.Trim(); sellernotes.Professor = addnotesviewmodel.Professor.Trim(); sellernotes.IsPaid = addnotesviewmodel.IsPaid; if (sellernotes.IsPaid) { sellernotes.SellingPrice = addnotesviewmodel.SellingPrice; } else { sellernotes.SellingPrice = 0; } sellernotes.CreatedDate = DateTime.Now; sellernotes.CreatedBy = user.UserID; sellernotes.IsActive = true; // add note in database and save _dbcontext.SellerNotes.Add(sellernotes); _dbcontext.SaveChanges(); // get seller note sellernotes = _dbcontext.SellerNotes.Find(sellernotes.ID); // if display picture is not null then save picture into directory and directory path into database if (addnotesviewmodel.DisplayPicture != null) { string displaypicturefilename = System.IO.Path.GetFileName(addnotesviewmodel.DisplayPicture.FileName); string displaypicturepath = "~/Members/" + user.UserID + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); sellernotes.DisplayPicture = displaypicturepath + displaypicturefilename; addnotesviewmodel.DisplayPicture.SaveAs(displaypicturefilepath); } // if note preview is not null then save picture into directory and directory path into database if (addnotesviewmodel.NotesPreview != null) { string notespreviewfilename = System.IO.Path.GetFileName(addnotesviewmodel.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.UserID + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); sellernotes.NotesPreview = notespreviewpath + notespreviewfilename; addnotesviewmodel.NotesPreview.SaveAs(notespreviewfilepath); } // update note preview path and display picture path and save changes _dbcontext.SellerNotes.Attach(sellernotes); _dbcontext.Entry(sellernotes).Property(x => x.DisplayPicture).IsModified = true; _dbcontext.Entry(sellernotes).Property(x => x.NotesPreview).IsModified = true; _dbcontext.SaveChanges(); // attachement files foreach (HttpPostedFileBase file in addnotesviewmodel.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 SellerNotesAttachment notesattachements = new SellerNotesAttachment { NoteID = sellernotes.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, CreatedDate = DateTime.Now, CreatedBy = user.UserID, IsActive = true }; // save seller notes attachement _dbcontext.SellerNotesAttachments.Add(notesattachements); _dbcontext.SaveChanges(); } } return(RedirectToAction("Dashboard", "SellYourNotes")); } // if model state is not valid else { // create object of addnotesviewmodel AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = _dbcontext.NoteCategories.Where(x => x.IsActive == true).ToList(), NoteTypeList = _dbcontext.NoteTypes.Where(x => x.IsActive == true).ToList(), CountryList = _dbcontext.Countries.Where(x => x.IsActive == true).ToList() }; return(View(viewModel)); } }
public ActionResult AddNotes(AddNotesViewModel xyz) { if (ModelState.IsValid) { // create seller note object SellerNotes abc = new SellerNotes(); var user = Context.Users.FirstOrDefault(x => x.Email == User.Identity.Name); abc.Status = Context.ReferenceData.Where(x => x.Value.ToLower() == "Draft").Select(x => x.ID).FirstOrDefault(); abc.SellerID = user.UserId; abc.Title = xyz.Title.Trim(); abc.Category = xyz.Category; abc.NoteType = xyz.NoteType; abc.NumberofPages = xyz.NumberofPages; abc.Description = xyz.Description.Trim(); abc.UniversityName = xyz.UniversityName.Trim(); abc.Country = xyz.Country; abc.Course = xyz.Course.Trim(); abc.CourseCode = xyz.CourseCode.Trim(); abc.Professor = xyz.Professor.Trim(); abc.IsPaid = xyz.IsPaid; if (abc.IsPaid) { abc.SellingPrice = xyz.SellingPrice; } else { abc.SellingPrice = 0; } abc.IsActive = true; Context.SellerNotes.Add(abc); Context.SaveChanges(); // // add note in database and save // get seller note abc = Context.SellerNotes.Find(abc.ID); // if display picture is not null then save picture into directory and directory path into database if (xyz.DisplayPicture != null) { string displaypicturefilename = System.IO.Path.GetFileName(xyz.DisplayPicture.FileName); string displaypicturepath = "~/Members/" + user.UserId + "/" + abc.ID + "/"; CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); abc.DisplayPicture = displaypicturepath + displaypicturefilename; xyz.DisplayPicture.SaveAs(displaypicturefilepath); } // if note preview is not null then save picture into directory and directory path into database if (xyz.NotesPreview != null) { string notespreviewfilename = System.IO.Path.GetFileName(xyz.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.UserId + "/" + abc.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); abc.NotesPreview = notespreviewpath + notespreviewfilename; xyz.NotesPreview.SaveAs(notespreviewfilepath); } // update note preview path and display picture path and save changes //Context.SellerNotes.Add(abc); //Context.Entry(abc).Property(x => x.DisplayPicture).IsModified = true; //Context.Entry(abc).Property(x => x.NotesPreview).IsModified = true; Context.SaveChanges(); // attachement files foreach (HttpPostedFileBase file in xyz.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 + "/" + abc.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); // create object of sellernotesattachement SellerNotesAttachements notesattachements = new SellerNotesAttachements { NotesID = abc.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, IsActive = true }; // save seller notes attachement Context.SellerNotesAttachements.Add(notesattachements); Context.SaveChanges(); } } return(RedirectToAction("Addnotes", "Addnotes")); } // if model state is not valid else { // create object of xyz AddNotesViewModel viewModel = new AddNotesViewModel { NoteCategoryList = Context.NoteCategories.ToList(), NoteTypeList = Context.NoteTypes.ToList(), CountryList = Context.Countries.ToList() }; return(View(viewModel)); } }
public ActionResult AddNotes(AddNotesViewModel note, string Command) { ViewBag.navclass = "white-nav"; ViewBag.SellNotes = "active"; Users user = db.Users.FirstOrDefault(x => x.Email == User.Identity.Name); //check user enter the profile details or not bool temp = db.UserProfile.Any(x => x.UserID == user.ID); if (!temp) { return(RedirectToAction("MyProfile", "UserProfile")); } if (user != null && ModelState.IsValid) { SellerNotes sellernotes = new SellerNotes(); sellernotes.SellerID = user.ID; sellernotes.ActionedBy = user.ID; sellernotes.Title = note.Title; sellernotes.Status = Command == "Save" ? 6 : 7; //6 for draft & 7 for submit for review sellernotes.Category = note.Category; sellernotes.NoteType = note.NoteType; sellernotes.NumberofPages = note.NumberofPages; sellernotes.Description = note.Description; sellernotes.UniversityName = note.UniversityName; sellernotes.Country = note.Country; sellernotes.Course = note.Course; sellernotes.CourseCode = note.CourseCode; sellernotes.Professor = note.Professor; sellernotes.IsPaid = note.IsPaid; sellernotes.SellingPrice = sellernotes.IsPaid == false ? 0 : note.SellingPrice; sellernotes.CreatedDate = DateTime.Now; sellernotes.CreatedBy = user.ID; sellernotes.IsActive = true; if (sellernotes.IsPaid) { if (sellernotes.SellingPrice == null || sellernotes.SellingPrice < 1) { ModelState.AddModelError("SellingPrice", "Enter valid Selling price"); AddNotesViewModel viewModel = GetData(); return(View(viewModel)); } } db.SellerNotes.Add(sellernotes); db.SaveChanges(); sellernotes = db.SellerNotes.Find(sellernotes.ID); if (note.DisplayPicture == null) { //get default file name string defaultFilename = Directory.GetFiles(Server.MapPath("~/Content/image/default-note-img/")).FirstOrDefault(); string finalfilename = System.IO.Path.GetFileName(defaultFilename); //defalt path if path is null string userprofilepath = "~/Content/image/default-note-img/"; sellernotes.DisplayPicture = userprofilepath + finalfilename; //store path } if (note.DisplayPicture != null) { string displaypicturefilename = System.IO.Path.GetFileName(note.DisplayPicture.FileName); //get filename string displaypicturepath = "~/Members/" + user.ID + "/" + sellernotes.ID + "/"; //path for save file CreateDirectoryIfMissing(displaypicturepath); string displaypicturefilepath = Path.Combine(Server.MapPath(displaypicturepath), displaypicturefilename); sellernotes.DisplayPicture = displaypicturepath + displaypicturefilename; note.DisplayPicture.SaveAs(displaypicturefilepath); } if (note.NotesPreview != null) { string notespreviewfilename = System.IO.Path.GetFileName(note.NotesPreview.FileName); string notespreviewpath = "~/Members/" + user.ID + "/" + sellernotes.ID + "/"; CreateDirectoryIfMissing(notespreviewpath); string notespreviewfilepath = Path.Combine(Server.MapPath(notespreviewpath), notespreviewfilename); sellernotes.NotesPreview = notespreviewpath + notespreviewfilename; note.NotesPreview.SaveAs(notespreviewfilepath); } db.SellerNotes.Attach(sellernotes); db.Entry(sellernotes).Property(x => x.DisplayPicture).IsModified = true; db.Entry(sellernotes).Property(x => x.NotesPreview).IsModified = true; db.SaveChanges(); //uploaded multiple files string notesattachementpath = "~/Members/" + user.ID + "/" + sellernotes.ID + "/Attachements/"; CreateDirectoryIfMissing(notesattachementpath); foreach (var file in note.UploadNotes) { string notesattachementfilename = System.IO.Path.GetFileName(file.FileName); string notesattachementfilepath = Path.Combine(Server.MapPath(notesattachementpath), notesattachementfilename); file.SaveAs(notesattachementfilepath); SellerNotesAttachements notesattachements = new SellerNotesAttachements { NoteID = sellernotes.ID, FileName = notesattachementfilename, FilePath = notesattachementpath, CreatedDate = DateTime.Now, CreatedBy = user.ID, IsActive = true }; db.SellerNotesAttachements.Add(notesattachements); db.SaveChanges(); } db.Dispose(); return(RedirectToAction("AddNotes", "SellNotes")); } else { if (note.IsPaid) { if (note.SellingPrice == null) { ModelState.AddModelError("SellingPrice", "Selling price is required"); } } AddNotesViewModel viewModel = GetData(); return(View(viewModel)); } }