public async Task <IActionResult> Profile() { Userinfo theUser = new Userinfo(); try { string userID = User.FindFirstValue(ClaimTypes.NameIdentifier);//gets userID, don't know a better way. If you want to find a better way, you can replace this. var users = from u in _context.Userinfo select u; users = users.Where(u => u.Id.Contains(userID)); if (users.Count() == 0) { return(RedirectToAction(nameof(Create))); } foreach (var user in users) { theUser = user; } } catch (Exception ex) { return(NotFound()); } theUser.Lastloggedin = DateTime.Now; try { _context.Update(theUser); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserinfoExists(theUser.Id)) { return(NotFound()); } else { throw; } } int age = 0; age = DateTime.Now.Year - theUser.Birthday.Year; if (DateTime.Now.DayOfYear < theUser.Birthday.DayOfYear) { age = age - 1; } ViewBag.Age = age; ViewBag.Birthday = theUser.Birthday.ToString("MM/dd/yyyy"); return(View(theUser)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Calories,DateEntered,UserId")] Foodinfo foodinfo) { if (id != foodinfo.Id) { return(NotFound()); } if (ModelState.IsValid) { bool emptyInput = false; ViewBag.NameError = "Nothing"; ViewBag.CalorieError = "Nothing"; if (foodinfo.Name == null || foodinfo.Name.Trim().Length == 0) { emptyInput = true; ViewBag.NameError = "Please enter an food."; } if (foodinfo.Calories == null || foodinfo.Calories <= 0) { emptyInput = true; ViewBag.CalorieError = "Please add calories."; } if (emptyInput) { ViewData["UserId"] = new SelectList(_context.AspNetUsers, "Id", "Id", foodinfo.UserId); return(View(foodinfo)); } try { _context.Update(foodinfo); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FoodinfoExists(foodinfo.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["UserId"] = new SelectList(_context.AspNetUsers, "Id", "Id", foodinfo.UserId); return(View(foodinfo)); }