Пример #1
0
        public IActionResult Create(AdModel model)
        {
            if (ModelState.IsValid)
            {
                // string title, decimal price, string location, string descritpion, string email, string phone

                var ad = new Ad()
                {
                    CategoryId  = model.CategoryId,
                    CreatedOn   = DateTime.Now,
                    Description = model.Description,
                    Email       = model.Email,
                    Location    = model.Location,
                    Price       = model.Price,
                    Telephone   = model.Phone,
                    Title       = model.Title,
                    Photo       = model.Photo
                };

                _ads.Create(ad);

                // ja viss OK, pārsūtām uz sludinājumu sadaļu
                return(RedirectToAction(nameof(MyAds), new { id = model.CategoryId }));
            }

            // kategorijas nepieciešams atlasīt arī POST pieprasījumā
            model.Categories = _categories.GetAll();
            return(View(model));
        }
Пример #2
0
 [HttpPost]                                 //post metodei ir nepieciešams norādīt iekavās, ko izsauc
 public IActionResult Create(AdModel model) //nosauc vienādi, jo atbilst vienam cshtml failam
 {
     if (ModelState.IsValid)
     {
         var ad = new Ad()
         {
             CategoryId  = model.CategoryId,
             CreatedOn   = DateTime.Now,
             Description = model.Description,
             Email       = model.Email,
             Location    = model.Location,
             Price       = model.Price,
             Telephone   = model.Telephone,
             Title       = model.Title,
             Photo       = model.Photo
         };
         _ads.Create(ad);
         //ja viss ok, pārsūtām uz sludinājumu sadaļu
         return(RedirectToAction(nameof(Index), new { id = model.CategoryId }));
     }
     //kategorijas nepieciešams atlasīt arī POST pieprasījumā
     model.Categories = _categories.GetAll();
     return(View(model));
 }