Skip to content

Instantly share code, notes, and snippets.

@Dark-Rex01
Created December 31, 2022 05:47
Show Gist options
  • Save Dark-Rex01/3f34b4aa3b8374daa16a7f69c9a8be61 to your computer and use it in GitHub Desktop.
Save Dark-Rex01/3f34b4aa3b8374daa16a7f69c9a8be61 to your computer and use it in GitHub Desktop.
namespace Repository.Controllers
{
[Route("api/User")]
public class UserController : Controller
{
private IUserRepository _userRepository;
public userController()
{
_userRepository = new UserRepository(new UserDBContext());
}
public UserController(IUserRepository userRepository)
{
_userRepository = userRepository;
}
[HttpGet]
public ActionResult Index()
{
var model = _userRepository.GetAll();
return View(model);
}
[HttpGet]
public ActionResult AddUser ()
{
return View();
}
[HttpPost]
public ActionResult AddUser (Usermodel)
{
if (ModelState.IsValid)
{
_UserRepository.Insert(model);
_UserRepository.Save();
return RedirectToAction("Index", "User");
}
return View();
}
[HttpGet]
public ActionResult EditUser (int UserId)
{
Employee model = _userRepository.GetById(UserId);
return View(model);
}
[HttpPost]
public ActionResult EditUser (User model)
{
if (ModelState.IsValid)
{
_userRepository.Update(model);
_userRepository.Save();
return RedirectToAction("Index", "User");
}
else
{
return View(model);
}
}
[HttpGet]
public ActionResult DeleteUser (int UserId)
{
User model = _userRepository.GetById(UserId);
return View(model);
}
[HttpPost]
public ActionResult Delete(int UserID)
{
_UserRepository.Delete(UserID);
_UserRepository.Save();
return RedirectToAction("Index", "User");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment