[英]Asp.net Core Razor Attendance Create Page is creating new employee while saving attendance time of an employee. How to fix it?
Database Used: MySql Server
DotNet Core Version = 2.2
Platform: Windows 10 IIS
當我嘗試保存現有員工的出勤情況時,出勤頁面正在嘗試創建一個在姓名字段中具有空值的新員工。 由於名稱字段設置為非空,因此失敗並顯示錯誤消息。
員工表
namespace payroll_razor_core.Models.repository
{
[Table("Employee")]
[Display(Name = "Employee",Description = "Stores Employee Basic Details.")]
public class Employee
{
[Column("Employee Id")]
[Key]
public string EmployeeId { get; set; }
public string EmployeeName =>
string.Concat(EmployeeFirstName,
string.IsNullOrEmpty(EmployeeMiddleName)?"":" "+EmployeeMiddleName,
string.IsNullOrEmpty(EmployeeLastName) ? "" : " " + EmployeeLastName
);
[Column("Employee First Name")]
[Display(Name = "First Name *")]
[MaxLength(200, ErrorMessage = "Exceeded Character Limit..!!")]
[RegularExpression(@"^[[A-Za-z+[\s]+[A-Za-z]+]*]*", ErrorMessage = "Can accept only characters..!!",
MatchTimeoutInMilliseconds = 1000)]
[Required(ErrorMessage = "Employee First Name is required..!!")]
public string EmployeeFirstName { get; set; }
[Column("Employee Middle Name")]
[Display(Name = "Middle Name *")]
[MaxLength(200, ErrorMessage = "Exceeded Character Limit..!!")]
[RegularExpression(@"^[[A-Za-z+[\s]+[A-Za-z]+]*]*", ErrorMessage = "Can accept only characters..!!",
MatchTimeoutInMilliseconds = 1000)]
public string EmployeeMiddleName { get; set; }
[Column("Employee Last Name")]
[Display(Name = "Last Name *")]
[MaxLength(200, ErrorMessage = "Exceeded Character Limit..!!")]
[RegularExpression(@"^[[A-Za-z+[\s]+[A-Za-z]+]*]*", ErrorMessage = "Can accept only characters..!!",
MatchTimeoutInMilliseconds = 1000)]
public string EmployeeLastName { get; set; }
public ICollection<AttendanceDailyRegister> AttendanceDailyRegisters { get; set; }
}
考勤表
[Table("Attendance")]
[Display(Name = "Attendance",Description = "Registers Employee Attendance")]
public class Attendance
{
[Key]
[Column("Attendance Id")]
[Display(Name = "Attendance Id")]
public int AttendanceId { get; set; }
[ForeignKey("EmployeeId")]
[Column("Employee")]
[Display(Name = "Employee")]
public string Employee { get; set; }
public bool Check{ get; set; }
[Column("AttendanceTime")]
[Display(Name = "Attendance Time",AutoGenerateField = true)]
[DisplayFormat(DataFormatString = "{0:dddd, dd/MM/yyyy, h:mm:ss tt}")]
[Timestamp]
public DateTime AttendanceTime { get; set; }
[ForeignKey("Employee")]
public virtual Employee Employees { get; set; }
}
考勤創建頁面
public class CreateModel : PageModel
{
private readonly Data.payroll_app_context _context;
public CreateModel(Data.payroll_app_context context)
{
_context = context;
}
public IActionResult OnGet()
{
ViewData["Employee"] = new SelectList(_context.Employee, "EmployeeId", "EmployeeName");
return Page();
}
[BindProperty]
public AttendanceDailyRegister AttendanceDailyRegister { get; set; }
public async Task<IActionResult> OnPostAsync()
{
//Commented for catching errors.
/*if (!ModelState.IsValid)
{
return Page();
}*/
_context.AttendanceDailyRegister.Add(AttendanceDailyRegister);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
考勤 Razor CSHTML 頁面
@page
@model razor.Pages.attendance.CreateModel
@{
ViewData["Title"] = "Create";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h1>Create</h1>
<h4>Attendance</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Attendance.Employee" class="control-label"></label>
<select asp-for="Attendance.Employee" class ="form-control" asp-items="ViewBag.Employee"></select>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="Attendance.Check" /> @Html.DisplayNameFor(model => model.Attendance.Check)
</label>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
在此頁面中,同時保存現有員工的新出勤時間,即創建新員工。 我無法修復它。 請幫我。
一換考勤表,問題就解決了。 雖然我不清楚它是如何解決的。
[Table("Attendance")]
[Display(Name = "Attendance",Description = "Registers Employee Attendance")]
public class Attendance
{
private readonly Employee employee;
[Key]
[Column("Attendance Id")]
[Display(Name = "Attendance Id")]
public int AttendanceId { get; set; }
[ForeignKey("EmployeeId")]
[Column("Employee")]
[Display(Name = "Employee")]
public string Employee { get; set; }
public bool Check{ get; set; }
[Column("AttendanceTime")]
[Display(Name = "Attendance Time",AutoGenerateField = true)]
[DisplayFormat(DataFormatString = "{0:dddd, dd/MM/yyyy, h:mm:ss tt}")]
[Timestamp]
public DateTime AttendanceTime { get; set; }
[ForeignKey("Employee")]
public virtual Employee Employees => employee
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.