Hamza Shah

Hamza Shah

  • NA
  • 87
  • 21.4k

Show a message through a controller “Attendance is already marked”

Nov 12 2020 11:54 PM
I'm working on online attendance portal, In which I've set a condition in a controller that user can't marked attendance twice a day. They are only allowed to mark attendance once per day. So I want to show a message on view page "Create" that "Attendance is already marked" if employee is marking the attendance second time on a same date. I've set an alert message but I want to show a message on view page from where employee is marking the attendance .I've searched it a lot but can't find any better one.
 
Here's my Controller Code
  1. [Authorize]  
  2. public ActionResult Create()  
  3. {  
  4. Employee employee = JsonConvert.DeserializeObject<Employee>(User.Identity.Name);  
  5. return View(new Attendance() { Emp_Id = employee.Emp_Id });  
  6. }  
  7. [HttpPost]  
  8. public ActionResult Create(Attendance attendance)  
  9. {  
  10. if (ModelState.IsValid)  
  11. {  
  12. try  
  13. {  
  14. var attdate = attendance.Date;  
  15. var nextdate = attdate.AddDays(1);  
  16. var id = Convert.ToInt32(Session["UserID"]);  
  17. var isExist = db.Attendance.FirstOrDefault(i => i.Emp_Id == id && i.Date == attdate && i.Date < nextdate);  
  18. if (isExist != null)  
  19. {  
  20. //Here i set the alert but i want to show message on view page.  
  21. return Content("<script language='javascript' type='text/javascript'>alert('Your Attendance is Already Marked');</script>");  
  22. }  
  23. else  
  24. {  
  25. //var res = tempDate.Date;  
  26. db.Attendance.Add(attendance);  
  27. db.SaveChanges();  
  28. }  
  29. }  
  30. catch (Exception ex)  
  31. {  
  32. Console.WriteLine(ex.InnerException.Message);  
  33. }  
  34. }  
  35. return RedirectToAction("Index""Attendance");  
  36. }  

Answers (1)