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
- DateTime todayDate = Convert.ToDateTime(DateTime.Now.ToString("dd MM yyyy"));
- [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)
- {
- if (attendance.Date =! todayDate)
- {
- try
- {
- db.Attendance.Add(attendance);
- db.SaveChanges();
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- }
- return RedirectToAction("Index", "Attendance");
- }
And Here's my View Code
- @model AttendancePortal2.Models.Attendance
- @{
- ViewBag.Title = "Index";
- }
- <div class="container">
- <div class="navbar-header">
- <h2>Add New Entry</h2>
- </div>
- <div class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li>@Html.ActionLink("Log out", "LogOff", "Account")</li>
- </ul>
- </div>
- <hr />
- @using (Html.BeginForm("Create", "Attendance", FormMethod.Post))
- {
- <div class="row">
- @Html.HiddenFor(model => model.Emp_Id)
- <div class="col-md-3">
- <label>Date <span class="text-danger">*</span></label>
- <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">
- @Html.TextBoxFor(model => model.Date, new { @class = "form-control" })
- <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
- <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
- </div>
- <input type="hidden" id="dtp_input2" value="" /><br />
- </div>
- <div class="col-md-3">
- <label>Check In <span class="text-danger">*</span></label>
- <div class="input-group date form_time" data-date="" data-date-format="hh:ii" data-link-field="dtp_input3" data-link-format="hh:ii">
- @Html.TextBoxFor(model => model.CheckIn, new { @class = "form-control" })
- <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
- <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
- </div>
- <input type="hidden" id="dtp_input1" value="" /><br />
- </div>
- <div class="col-md-3">
- <label>Check Out</label>
- <div class="input-group date form_time" data-date="" data-date-format="hh:ii" data-link-field="dtp_input3" data-link-format="hh:ii">
- @Html.TextBoxFor(model => model.CheckOut, new { @class = "form-control" })
- <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
- <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
- </div>
- <input type="hidden" id="dtp_input3" value="" /><br />
- </div>
- <div class="col-md-3">
- <label>Short Leave (Hours)</label>
- @Html.TextBoxFor(model => model.ShortLeave, new { @class = "form-control", Type = "number" })
- </div>
- </div>
- <div class="row">
- <div class="col-md-12">
- <input type="submit" class="btn btn-primary" value="Mark Attendance" />
- @Html.ActionLink("My Attendance", "Index", "Attendance")
- </div>
- </div>
- }
- </div>