ASP.NET MVC(Model-View-Controller)是一种用于构建Web应用程序的框架,它基于MVC设计模式。MVC模式将应用程序分为三个主要组件:
ASP.NET MVC主要分为以下几种类型:
ASP.NET MVC适用于各种Web应用程序的开发,包括但不限于:
假设我们要实现一个简单的员工考勤系统,允许员工每天标记一次考勤。以下是一个基本的实现思路:
CREATE TABLE Employees (
EmployeeId INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100) NOT NULL
);
CREATE TABLE Attendance (
AttendanceId INT PRIMARY KEY IDENTITY(1,1),
EmployeeId INT,
Date DATE,
Status NVARCHAR(50),
FOREIGN KEY (EmployeeId) REFERENCES Employees(EmployeeId)
);
public class AttendanceController : Controller
{
private readonly ApplicationDbContext _context;
public AttendanceController(ApplicationDbContext context)
{
_context = context;
}
// GET: Attendance/MarkAttendance
public IActionResult MarkAttendance(int employeeId)
{
var employee = _context.Employees.Find(employeeId);
if (employee == null)
{
return NotFound();
}
var attendance = new Attendance
{
EmployeeId = employeeId,
Date = DateTime.Today,
Status = "Present"
};
_context.Attendance.Add(attendance);
_context.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
@model List<Attendance>
<table>
<thead>
<tr>
<th>Employee Name</th>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
@foreach (var attendance in Model)
{
<tr>
<td>@attendance.Employee.Name</td>
<td>@attendance.Date.ToString("yyyy-MM-dd")</td>
<td>@attendance.Status</td>
</tr>
}
</tbody>
</table>
原因:
解决方法:
解决方法: 可以在标记考勤之前检查当天是否已经存在该员工的考勤记录。
public IActionResult MarkAttendance(int employeeId)
{
var today = DateTime.Today;
var existingAttendance = _context.Attendance.FirstOrDefault(a => a.EmployeeId == employeeId && a.Date == today);
if (existingAttendance != null)
{
TempData["ErrorMessage"] = "You have already marked your attendance for today.";
return RedirectToAction("Index", "Home");
}
// 继续保存考勤记录的逻辑
}
希望这些信息对你有所帮助!如果有更多具体问题,请随时提问。
领取专属 10元无门槛券
手把手带您无忧上云