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
JavaScript
x
42
42
1
[Authorize]
2
public ActionResult Create()
3
{
4
Employee employee = JsonConvert.DeserializeObject<Employee>(User.Identity.Name);
5
6
return View(new Attendance() { Emp_Id = employee.Emp_Id });
7
}
8
9
[HttpPost]
10
public ActionResult Create(Attendance attendance)
11
{
12
13
if (ModelState.IsValid)
14
{
15
try
16
{
17
var attdate = attendance.Date;
18
var nextdate = attdate.AddDays(1);
19
var id = Convert.ToInt32(Session["UserID"]);
20
var isExist = db.Attendance.FirstOrDefault(i => i.Emp_Id == id && i.Date == attdate && i.Date < nextdate);
21
22
if (isExist != null)
23
{
24
//Here i set the alert but i want to show message on view page.
25
return Content("<script language='javascript' type='text/javascript'>alert('Your Attendance is Already Marked');</script>");
26
}
27
else
28
{
29
//var res = tempDate.Date;
30
db.Attendance.Add(attendance);
31
db.SaveChanges();
32
}
33
}
34
catch (Exception ex)
35
{
36
Console.WriteLine(ex.InnerException.Message);
37
}
38
}
39
40
return RedirectToAction("Index", "Attendance");
41
}
42
Advertisement
Answer
Controller:
JavaScript
1
5
1
if (isExist != null)
2
{
3
TempData["Msg"] = "Your Attendance is Already Marked'"
4
}
5
View:
JavaScript
1
11
11
1
<body>
2
@if (TempData["Msg"] != null)
3
{
4
<script type="text/javascript">
5
window.onload = function () {
6
alert(@TempData["Msg"]);
7
};
8
</script>
9
}
10
</body>
11