Asp.net Core Razor出勤创建页面正在创建新雇员,同时节省了雇员的出勤时间。如何解决?

问题描述 投票:0回答:1
Database Used: MySql Server
DotNet Core Version = 2.2
Platform: Windows 10 IIS

[当我尝试保存现有员工的出勤时,出勤页面正在尝试使用名称字段中的空值创建新员工。由于名称字段设置为非null,因此失败并显示错误消息。

员工表

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");
        }
    }

考勤剃刀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");}
}

在此页面中,同时节省了现有员工的新上班时间,从而创建了新员工。我无法解决。请帮助我。

c# asp.net-core asp.net-mvc-4 razor razor-2
1个回答
0
投票
我更改了出勤表的那一刻,问题就解决了。尽管我不清楚如何解决。

[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 }

© www.soinside.com 2019 - 2024. All rights reserved.