Skip to content
Advertisement

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

I’m working on an online attendance portal, In which I’ve set a condition in a controller that users can’t mark attendance twice a day. They are only allowed to mark attendance once per day. So I want to show a message on the view page “Create” that “Attendance is already marked” if an employee is marking the attendance a second time on the same date. I’ve set an alert message but I want to show a message on the view page from where the employee is marking the attendance. I’ve searched for 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)
                    {
                   //Here i set the alert but i want to show message on view page.
                        return Content("<script language='javascript' type='text/javascript'>alert('Your Attendance is Already Marked');</script>");
                    }
                    else
                    {
                        //var res = tempDate.Date;
                        db.Attendance.Add(attendance);
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }

            return RedirectToAction("Index", "Attendance");
        }

Advertisement

Answer

Controller:

if (isExist != null)
{
   TempData["Msg"] = "Your Attendance is Already Marked'"
}

View:

<body>
@if (TempData["Msg"] != null)  
{  
     <script type="text/javascript">  
         window.onload = function () {  
             alert(@TempData["Msg"]);  
          };  
      </script>  
}  
</body>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement