public ActionResult Create(PortfolioUsers pu) { //double check authentication if (Authenticate.IsAuthenticated()) { try { pu.Privacies = new PrivacyList(); pu.Privacies.Load(); PortfolioList portfolios = new PortfolioList(); User userin = System.Web.HttpContext.Current.Session["user"] as User; string username = userin.Username; pu.Portfolio.UserId = userin.Id; pu.Portfolio.UserEmail = userin.Email; portfolios.LoadbyUser(userin); if (pu.Portfolio.Name == null) { ModelState.AddModelError(string.Empty, "Portfolio requires a name!"); } else { foreach (Portfolio p in portfolios) { if (pu.Portfolio.Name == p.Name) { ModelState.AddModelError(string.Empty, "Portfolio name already exists!"); } } } UploadedImage ui = new UploadedImage { FilePath = pu.Portfolio.PortfolioImage, Fileupload = pu.Fileupload, UserName = username, ObjectType = "Portfolio", ObjectName = pu.Portfolio.Name }; string fp = ui.Upload(); // fp will return null if no upload file was choosen else use upload file to save to database if (fp != null) { pu.Portfolio.PortfolioImage = fp; } else { // I honestly don't know when this would happen but just in case ModelState.AddModelError(string.Empty, "Portfolio Image could not found"); } if (!ModelState.IsValid) { pu.Portfolio = new Portfolio(); pu.User = new User(); pu.Privacies = new PrivacyList(); pu.Privacies.Load(); pu.User.LoadById(userin.Id); return View(pu); } pu.Portfolio.Insert(); return RedirectToAction("EditPortfolios", "UserProfile", new { returnurl = HttpContext.Request.Url }); } catch { return View(pu); } } else { return RedirectToAction("Index", "Login", new { returnurl = HttpContext.Request.Url }); } }
public ActionResult EditPortfolio(Guid id, UserProfile up) { if (Authenticate.IsAuthenticated()) { try { User userin = System.Web.HttpContext.Current.Session["user"] as User; PortfolioList Portfolios = new PortfolioList(); Portfolios.LoadbyUser(userin); up.Portfolios = Portfolios; ProjectList projects = new ProjectList(); projects.LoadbyPortfolioID(id); PrivacyList privacies = new PrivacyList(); privacies.Load(); up.Privacies = privacies; up.Projects = projects; string username = userin.Username; if (up.Portfolio.Name == null) { ModelState.AddModelError(string.Empty, "Portfolio requires a name!"); } else { foreach (Portfolio p in Portfolios) { if (up.Portfolio.Name == p.Name) { if (up.Portfolio.Id != p.Id) { ModelState.AddModelError(string.Empty, "Another portfolio already exists with this name!"); } } } } UploadedImage ui = new UploadedImage { FilePath = up.Portfolio.PortfolioImage, Fileupload = up.Fileupload, UserName = username, ObjectType = "Portfolio", ObjectName = up.Portfolio.Name }; string fp = ui.Upload(); // fp will return null if no upload file was choosen else use upload file to save to database if (fp != null) { up.Portfolio.PortfolioImage = fp; } else { up.Portfolio.PortfolioImage = null; } if (!ModelState.IsValid) { up.Privacies = new PrivacyList(); up.User = new User(); up.User.LoadById(userin.Id); up.Privacies.Load(); up.Projects.LoadbyPortfolioID(up.Portfolio.Id); return(View(up)); } up.Portfolio.Update(); return(RedirectToAction("EditPortfolios")); } catch { return(View(up)); } } else { return(RedirectToAction("Index", "Login", new { returnurl = HttpContext.Request.Url })); } }