public ActionResult RegisterPost(HttpPostedFileBase Photo)
        {
            var item = new Student() { UserRole = AccountStatus.Student };

            try
            {
                TryUpdateModel(item, "RegisterItem", new string[] { "Login", "Name", "Password", "Group", "Course", "Age" });

                if (db.Users.Count(i => i.Login == item.Login) > 0)
                    throw new ArgumentException("Пользователь с таким e-mail уже существует.");

                if (ModelState.IsValid)
                {
                    if (Photo != null && Photo.IsImage())
                    {
                        item.PhotoMime = Photo.ContentType;
                        item.Photo = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.PhotoMime);
                        Photo.InputStream.Read(item.Photo, 0, Photo.ContentLength);
                    }

                    db.Students.Add(item);
                    db.SaveChanges();
                    System.Web.HttpContext.Current.Session.Authorize(item);
                    return RedirectToAction("Profile");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }
            return View(new AccountViewModel(item));
        }
Пример #2
0
        public ActionResult AdvertCreate(Advert model,HttpPostedFileBase image)
        {
            var cats = MongoDbContext.db.GetCollection<Category>("Category").Find(n => true).ToListAsync().GetAwaiter().GetResult();
            
            ViewBag.CategoryId = new SelectList(cats, "Id", "Name");
            
                 //attempt to add the advert
            if(ModelState.IsValid)
            {
                bool addSucceded;

                try
                {
                    if (image != null && image.IsImage())
                    {
                        image.SaveAs(Server.MapPath("~/Content/Images/") + image.FileName);
                        model.Photo = image.FileName;
                    }
                    else
                        model.Photo = null;
   
                    model.Id = ObjectId.GenerateNewId().ToString();
                    model.PublicationDate = DateTime.Now;
                    model.ViewNumber = 0;
                    //model.VIP = false;
                    model.UserId = MongoDbContext.getUser(User.Identity.Name).Id.ToString();
                    context.AddAdvert(model);
                    addSucceded = true;
                }
                catch (Exception e)
                { 
                    addSucceded = false;
                }
                if (addSucceded)
                    return RedirectToAction("AdvertCreate", new { Message = ManageMessageId.ChangePasswordSuccess });
                else
                    ModelState.AddModelError("", "Ошибка добавления объявления");
            }

                

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult EditPost(int? id, HttpPostedFileBase Photo)
        {
            Exhibit item = null;

            try
            {
                if (id.HasValue)
                {
                    item = db.Exhibits.SingleOrDefault(i => i.Id == id);
                    if (item == null)
                        throw new ArgumentException("Экспонат не найден.");
                }
                else
                {
                    item = new Exhibit();
                }

                TryUpdateModel(item, "Item", new[] { "Name", "Description", "Location", "Code" });

                if (ModelState.IsValid)
                {
                    if (Photo != null && Photo.IsImage())
                    {
                        item.ApplicationType = Photo.ContentType;
                        item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
                        Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
                    }

                    if (id == null)
                        db.Exhibits.Add(item);
                    else
                        db.Entry<Exhibit>(item).State = EntityState.Modified;

                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }

            return View(new ExhibitViewModel(db, item));
        }
 public ActionResult ChangeImage(HttpPostedFileBase Photo)
 {
     var user = db.Users.Find(System.Web.HttpContext.Current.Session.GetUser().Id);
     if (Photo.IsImage())
     {
         user.PhotoMime = Photo.ContentType;
         user.Photo = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(user.PhotoMime);
         Photo.InputStream.Read(user.Photo, 0, Photo.ContentLength);
         db.Entry(user).State = System.Data.EntityState.Modified;
         db.SaveChanges();
     }
     return RedirectToAction("Profile");
 }
        public ActionResult UploadPicture(HttpPostedFileBase file, int albumId)
        {
            if (file == null)
            {
                throw new NullReferenceException("File is null!");
            }

            if (file.IsImage())
            {
                var image = file.HttpPostedFileBaseToImage().ImageToByteArray();

                var albumPhoto = new AlbumPhoto()
                {
                    Photo = image,
                    Likes = new List<Like>(),
                    Comments = new List<Comment>(),
                    AlbumId = albumId
                };

                this.Data.AlbumPhotos.Add(albumPhoto);
                this.Data.SaveChanges();
            }
            else
            {
                throw new ArgumentException("File is not image");
            }
            return RedirectToAction("PhotosInAlbum", new { id = albumId });
        }
Пример #6
0
        public ActionResult Edit(Advert model, HttpPostedFileBase image)
        {
            var cats = MongoDbContext.db.GetCollection<Category>("Category").Find(n => true).ToListAsync().GetAwaiter().GetResult();
            var old = MongoDbContext.db.GetCollection<Advert>("Advert").Find(m => m.Id == model.Id).FirstAsync().GetAwaiter().GetResult();

            ViewBag.CategoryId = new SelectList(cats, "Id", "Name", model.CategoryId);

            //attempt to edit the advert
            if (ModelState.IsValid)
            {
                bool editSucceded;
                try
                {
                    if (image != null && image.IsImage())
                    {
                        image.SaveAs(Server.MapPath("~/Content/Images/") + image.FileName);
                        model.Photo = image.FileName;
                    }
                    else
                        model.Photo = old.Photo;
                    model.PublicationDate = old.PublicationDate;
                    model.ViewNumber = old.ViewNumber;
                    model.UserId = old.UserId;
                    //model.VIP = old.VIP;

                    MongoDbContext.db.GetCollection<Advert>("Advert").ReplaceOneAsync(m => m.Id == model.Id, model).GetAwaiter().GetResult();
                    editSucceded = true;
                }
                catch (Exception e)
                {
                    editSucceded = false;
                }
                if (editSucceded)
                    return RedirectToAction("Edit", new { Message = ManageMessageId.Success });
                else
                    ModelState.AddModelError("", "Ошибка добавления объявления");
            }



            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult ChangeArticleImage(int id, HttpPostedFileBase Photo)
        {
            var item = db.Articles.Find(id);
            if (item == null || item.Exhibit.Id == 0) //lazyloading fail
                return RedirectToAction("Index");

            if (Photo.IsImage())
            {
                if (Photo != null && Photo.IsImage())
                {
                    item.ApplicationType = Photo.ContentType;
                    item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
                    Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
                }
                db.Entry<Article>(item).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Articles", new { id = item.Exhibit.Id });
            }
            return RedirectToAction("Articles", new { id = item.Exhibit.Id });
        }
        public ActionResult EditArticlePost(int exhibitId, int? id, HttpPostedFileBase Photo)
        {
            Article item = null;
            Exhibit exhibit = null;

            try
            {
                exhibit = db.Exhibits.Find(exhibitId);
                if (exhibit == null)
                    throw new ArgumentException("Экспонат не найден.");

                if (id.HasValue)
                {
                    item = db.Articles.SingleOrDefault(i => i.Id == id);
                    if (item == null)
                        throw new ArgumentException("Материал не найден.");
                }
                else
                {
                    item = new Article() { Exhibit = exhibit };
                }

                TryUpdateModel(item, "Article", new[] { "Title", "Text" });

                if (ModelState.IsValid)
                {
                    if (Photo != null && Photo.IsImage())
                    {
                        item.ApplicationType = Photo.ContentType;
                        item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
                        Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
                    }

                    if (id == null)
                        db.Articles.Add(item);
                    else
                        db.Entry<Article>(item).State = EntityState.Modified;

                    db.SaveChanges();
                    return RedirectToAction("Articles", new { id = exhibit.Id });
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }

            return View(new ExhibitViewModel(db, exhibit, item));
        }
        public ActionResult ChangeImage(int id, HttpPostedFileBase Photo)
        {
            var item = db.Exhibits.Find(id);
            if (item == null)
                return RedirectToAction("Index");

            if (Photo.IsImage())
            {
                if (Photo != null && Photo.IsImage())
                {
                    item.ApplicationType = Photo.ContentType;
                    item.ApplicationData = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.ApplicationType);
                    Photo.InputStream.Read(item.ApplicationData, 0, Photo.ContentLength);
                }
                db.Entry<Exhibit>(item).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }