我在用剃刀页的服务器端验证的问题(ASP.Net核2.0)剃刀页面在服务器验证没有一些领域返回的
我有一个创建一个记录页面
http://localhost:56876/Entries/Create?employeeID=112345
我隐藏文件的代码为使用onGET样子:
[BindProperty]
public EntryLog EntryLog { get; set; }
public Employee Employee{ get; set; }
public string empID { get; set; }
public IActionResult OnGet(string employeeID)
{
empID = employeeID;
// for Selects for lookup tables
ViewData["ReasonId"] = new SelectList(_context.Reason, "Id", "Name");
ViewData["ReasonTypeId"] = new SelectList(_context.ReasonType, "Id", "Name");
return Page();
}
上面的代码只是正常工作和客户验证的作品完美,但我有业务逻辑验证,如果入场日期是今天的日期和雇佣日期之间的90,我不应该让条目被保存。
public async Task<IActionResult> OnPostAsync()
{
//Lookup current employeeID hiredDate. if is new employee do not continue.
Employee = await _context.Employee .FirstOrDefaultAsync(p => p.Id == EntryLog.EmployeeID);
//Server side validation
var age = DateTime.Today.Day - Employee.HireDate.Day;
if (age <= 90)
{
ModelState.AddModelError("NewEmp", "Emp is New Warning");
return Page();
}
if (!ModelState.IsValid)
{
return Page();
}
_context.EntryLog.Add(EntryLog);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
我的服务器端验证工程,但问题是,当我返回Page(); 表单被刷新,两个选择元素被清空,EmployeeID元素也被清空。
我的猜测是OnPostAsync()上的返回Page()没有调用OnGet()方法。
如何在不丢失信息的情况下保持原样?
您的方法没有任何模型? – Hadee