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");
}
}
Attendance Razor CS HTML Страница
@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");}
}
Здесь, на этой странице, при сохранении нового времени посещаемости существующего сотрудника, создается новый сотрудник. Я не могу это исправить. Пожалуйста, помогите мне.