コード例 #1
0
        public IActionResult Put(Hobby hobby)
        {
            if (hobby.Id == null || hobby.Id < 1)
            {
                return(BadRequest("Invalid member Id"));
            }

            var dbInfo = _ctx.Hobbies.Find(hobby.Id);

            if (dbInfo == null)
            {
                return(NotFound());
            }

            dbInfo.FullName            = hobby.FullName;
            dbInfo.FavoriteHobby       = hobby.FavoriteHobby;
            dbInfo.SecondFavoriteHobby = hobby.SecondFavoriteHobby;
            dbInfo.WhyYouLikeThem      = hobby.WhyYouLikeThem;
            _ctx.Hobbies.Update(dbInfo);
            var changes = _ctx.SaveChanges();

            if (changes > 0)
            {
                return(NoContent());
            }


            return(StatusCode(500, "Error occured on the server. Please try again in a few minutes."));
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "FoodId,Dish,Course,MakeAgain")] Food food)
        {
            if (ModelState.IsValid)
            {
                db.Foods.Add(food);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(food));
        }
コード例 #3
0
        public ActionResult Create([Bind(Include = "ShowId,Title,Genre,Type,Service")] Show show)
        {
            if (ModelState.IsValid)
            {
                db.Shows.Add(show);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(show));
        }
コード例 #4
0
        public ActionResult Create([Bind(Include = "BookId,Title,Author,Genre,Type")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
コード例 #5
0
 public IActionResult NewUser(User user)
 {
     if (ModelState.IsValid)
     {
         if (dbContext.users.Any(u => u.UserName == user.UserName))
         {
             ModelState.AddModelError("UserName", "UserName already in use");
             return(View("Index"));
         }
         // Initializing a PasswordHasher object, providing our User class as its
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         user.Password = Hasher.HashPassword(user, user.Password);
         //Save your user object to the database
         dbContext.Add(user);
         dbContext.SaveChanges();
         HttpContext.Session.SetInt32("Id", user.UserId);
         HttpContext.Session.SetString("FirstName", user.FirstName);
         return(RedirectToAction("AllHobbies"));
     }
     else
     {
         return(View("Index"));
     }
 }
コード例 #6
0
 public IActionResult Register(User submitted_info)
 {
     submitted_info.CreatedAt = DateTime.Now;
     submitted_info.UpdatedAt = DateTime.Now;
     if (ModelState.IsValid)
     {
         // If a User exists with provided email
         if (dbContext.users.Any(u => u.UserName == submitted_info.UserName))
         {
             // Manually add a ModelState error to the Email field, with provided
             // error message
             ModelState.AddModelError("UserName", "UserName already in use!");
             return(View("RegisterView"));
             // You may consider returning to the View at this point
         }
         //LOGIC FOR ADDING TO DB
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         submitted_info.Password = Hasher.HashPassword(submitted_info, submitted_info.Password);
         HttpContext.Session.SetInt32("ID", submitted_info.UserId);
         dbContext.Add(submitted_info);
         dbContext.SaveChanges();
         return(RedirectToAction("loginuser"));
     }
     else
     {
         Console.WriteLine("NOT VALID**************", ModelState.Keys);
         foreach (var error in ModelState)
         {
             if (error.Value.Errors.Count > 0)
             {
                 Console.WriteLine("err msg " + error.Value.Errors[0].ErrorMessage);
             }
         }
         return(View("RegisterView"));
     }
 }