简体   繁体   English

Asp.net Core Razor 考勤创建页面在创建新员工的同时节省员工的考勤时间。 如何解决?

[英]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

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

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM