コード例 #1
0
ファイル: AdvertController.cs プロジェクト: vnavka/AdvertSite
        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));
        }