Database Used: MySql Server
DotNet Core Version = 2.2
Platform: Windows 10 IIS
When I am trying to save an attendance of an existing employee, the attendance page is trying to create a new employee with null values in Name fields. Since name field is set to not null its failed and showing error message.
Employee Table
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; }
}
Attendance Table
[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; }
}
Attendance Create Page
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 CSHTML Page
@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");}
}
Here in this page while saving new attendance time of an existing employee, its creating a new employee. I Cannot fix it. Please help me.
The moment I changed the attendance table, the problem got solved. Though I don't clearly understand how it got solved.
[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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.