示例#1
0
        public ActionResult Create(CreatePhotoViewModels photo, HttpPostedFileBase photoUpload)
        {
            Photo photoToDB = PhotoMapper.MapCreatePhotoViewModel(photo);

            try
            {
                photoToDB.UserID = userID;
                photoToDB.Id     = Guid.NewGuid();
                photoToDB.Date   = DateTime.Now;
                photoToDB.Path   = $" /Photos/{photoUpload.FileName}";
                photoRepository.AddPhotoToDB(photoToDB);

                photoUpload.SaveAs(Path.Combine(Server.MapPath("~/Photos"), photoUpload.FileName));
                return(Content(photoUpload.FileName));
            }
            catch
            {
                var model  = PhotoMapper.MapCreatePhotoViewModel(photoToDB);
                var albums = albumRepository.GettAllAlbumsByUserID(userID);
                model.Albums.Add(new SelectListItem {
                    Text = "Uncategorized", Value = ""
                });
                albums.ForEach(x => model.Albums.Add(new SelectListItem {
                    Text = x.Name, Value = x.Id.ToString()
                }));

                return(Content(photoUpload.FileName));
            }
        }