我是ASP.NET MVC的新手。在我的考勤管理系统中,没有。每天标记出勤的员工人数。我想限制员工每天只标记一次出勤。我已经做了很多搜索,但没有找到准确的东西。
这是我的管理员代码来创建出勤
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");
}
这是我的查看代码
@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>
答案 0 :(得分:0)
尝试一下:
[HttpPost]
public ActionResult Create(Attendance attendance)
{
if (!ModelState.IsValid) return BadRequest("Not valid error");
if (attendance.Date =! todayDate)return BadRequest("Too late or too early");
var hasAttendance= db.Attendance.Any(i=> i.Emp_Id=attendance.EmpId
&& i.Date==todayDate);
if (hasAttendance) return BadRequest("It is done already");
try
{
db.Attendance.Add(attendance);
db.SaveChanges();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return RedirectToAction("Index", "Attendance");
}