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
- [Authorize]
- public ActionResult Create()
- {
- Employee employee = JsonConvert.DeserializeObject<Employee>(User.Identity.Name);
- return View(new Attendance() { Emp_Id = employee.Emp_Id });
- }
- [HttpPost]
- public ActionResult Create(Attendance attendance)
- {
- if (ModelState.IsValid)
- {
- try
- {
- var attdate = attendance.Date;
- var nextdate = attdate.AddDays(1);
- var id = Convert.ToInt32(Session["UserID"]);
- var isExist = db.Attendance.FirstOrDefault(i => i.Emp_Id == id && i.Date == attdate && i.Date < nextdate);
- if (isExist != null)
- {
-
- return Content("<script language='javascript' type='text/javascript'>alert('Your Attendance is Already Marked');</script>");
- }
- else
- {
-
- db.Attendance.Add(attendance);
- db.SaveChanges();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.InnerException.Message);
- }
- }
- return RedirectToAction("Index", "Attendance");
- }