Hamza Shah

Hamza Shah

  • NA
  • 87
  • 22.8k

Allow Employee to mark attendance once per day ASP.NET MVC

Nov 9 2020 5:32 AM
I'm new to ASP.NET MVC. In my Attendance management system, There are no. of employees marking their attendance on a daily basis. I want to restrict employee to mark only one attendance per day. I've done a lot of search but nothing find anything accurate.
 
Here's my Controller Code to Create Attendance
  1. DateTime todayDate = Convert.ToDateTime(DateTime.Now.ToString("dd MM yyyy"));  
  2. [Authorize]  
  3. public ActionResult Create() {  
  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. if (attendance.Date =! todayDate)  
  13. {  
  14. try  
  15. {  
  16. db.Attendance.Add(attendance);  
  17. db.SaveChanges();  
  18. }  
  19. catch (Exception ex)  
  20. {  
  21. throw;  
  22. }  
  23. }  
  24. }  
  25. return RedirectToAction("Index""Attendance");  
  26. }  
And Here's my View Code
  1. @model AttendancePortal2.Models.Attendance  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. }  
  5. <div class="container">  
  6. <div class="navbar-header">  
  7. <h2>Add New Entry</h2>  
  8. </div>  
  9. <div class="navbar-collapse collapse">  
  10. <ul class="nav navbar-nav">  
  11. <li>@Html.ActionLink("Log out""LogOff""Account")</li>  
  12. </ul>  
  13. </div>  
  14. <hr />  
  15. @using (Html.BeginForm("Create""Attendance", FormMethod.Post))  
  16. {  
  17. <div class="row">  
  18. @Html.HiddenFor(model => model.Emp_Id)  
  19. <div class="col-md-3">  
  20. <label>Date <span class="text-danger">*</span></label>  
  21. <div class="input-group date form_date" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">  
  22. @Html.TextBoxFor(model => model.Date, new { @class = "form-control" })  
  23. <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>  
  24. <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>  
  25. </div>  
  26. <input type="hidden" id="dtp_input2" value="" /><br />  
  27. </div>  
  28. <div class="col-md-3">  
  29. <label>Check In <span class="text-danger">*</span></label>  
  30. <div class="input-group date form_time" data-date="" data-date-format="hh:ii" data-link-field="dtp_input3" data-link-format="hh:ii">  
  31. @Html.TextBoxFor(model => model.CheckIn, new { @class = "form-control" })  
  32. <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>  
  33. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>  
  34. </div>  
  35. <input type="hidden" id="dtp_input1" value="" /><br />  
  36. </div>  
  37. <div class="col-md-3">  
  38. <label>Check Out</label>  
  39. <div class="input-group date form_time" data-date="" data-date-format="hh:ii" data-link-field="dtp_input3" data-link-format="hh:ii">  
  40. @Html.TextBoxFor(model => model.CheckOut, new { @class = "form-control" })  
  41. <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>  
  42. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>  
  43. </div>  
  44. <input type="hidden" id="dtp_input3" value="" /><br />  
  45. </div>  
  46. <div class="col-md-3">  
  47. <label>Short Leave (Hours)</label>  
  48. @Html.TextBoxFor(model => model.ShortLeave, new { @class = "form-control", Type = "number" })  
  49. </div>  
  50. </div>  
  51. <div class="row">  
  52. <div class="col-md-12">  
  53. <input type="submit" class="btn btn-primary" value="Mark Attendance" />  
  54. @Html.ActionLink("My Attendance""Index""Attendance")  
  55. </div>  
  56. </div>  
  57. }  
  58. </div>

Answers (1)