C# Asp.net核心Razor考勤创建页面在创建新员工的同时节省员工的考勤时间。如何修复它?
当我试图保存现有员工的考勤时,考勤页面试图创建一个新员工,其姓名字段中的值为空。由于名称字段设置为不为空,因此其失败并显示错误消息 员工表C# Asp.net核心Razor考勤创建页面在创建新员工的同时节省员工的考勤时间。如何修复它?,c#,asp.net-core,asp.net-mvc-4,razor,razor-2,C#,Asp.net Core,Asp.net Mvc 4,Razor,Razor 2,当我试图保存现有员工的考勤时,考勤页面试图创建一个新员工,其姓名字段中的值为空。由于名称字段设置为不为空,因此其失败并显示错误消息 员工表 Database Used: MySql Server DotNet Core Version = 2.2 Platform: Windows 10 IIS 考勤创建页面 [Table("Attendance")] [Display(Name = "Attendance",Description = "Registers Employee Atte
Database Used: MySql Server
DotNet Core Version = 2.2
Platform: Windows 10 IIS
考勤创建页面
[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; }
}
公共类CreateModel:PageModel
{
private readonly Data.payroll\u app\u context\u context;
公共CreateModel(Data.payroll\u app\u上下文)
{
_上下文=上下文;
}
公共IActionResult OnGet()
{
ViewData[“Employee”]=新建选择列表(_context.Employee,“EmployeeId”,“EmployeeName”);
返回页();
}
[BindProperty]
公众参与度DailyRegister参与度DailyRegister{get;set;}
公共异步任务OnPostAsync()
{
//对捕获错误进行了注释。
/*如果(!ModelState.IsValid)
{
返回页();
}*/
_Add(AttendanceDailyRegister);
wait_context.SaveChangesAsync();
返回页首(“/索引”);
}
}
考勤管理器CSHTML页面
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");
}
}
@page
@model razor.Pages.Attention.CreateModel
@{
ViewData[“标题”]=“创建”;
Layout=“~/Pages/Shared/_Layout.cshtml”;
}
创造
出勤
@DisplayNameFor(model=>model.attention.Check)
返回列表
@节脚本{
@{wait Html.RenderPartialAsync(“_validationScript”);}
}
在此页面中,在保存现有员工的新出勤时间的同时,创建新员工。我修不好。请帮帮我。我刚换了考勤表,问题就解决了。虽然我不清楚它是怎么解决的
@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");}
}
\u context.AttendanceDailyRegister.Add(AttendanceDailyRegister)
将实体添加到上下文中。它不会自动更新现有实体。您必须首先检索现有实体并更新该实体。还请注意,您不应该将数据库模型用作请求参数,因为这将有效地打开许多安全漏洞。对从Razor页面检索的参数使用单独的模型。确定。我将使用单独的模型来保存数据。但这里的主要问题是,我并不是在试图更新现有的数据。我正在尝试根据现有员工的日期保存新的考勤数据。而是创建新员工。然后确保未设置新实体的Id,以便将其理解为新实体。谢谢您的帮助。
@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
}