(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()を返すときです。 フォームがリフレッシュされ、2つのselect要素が空になり、EmployeeID要素も空になります。
私の推測では、OnPostAsync()のPage()がOnGet()メソッドを呼び出していないと思います。
どうすれば情報を失うことなくページをそのまま維持できますか?
あなたのメソッドのモデルはありませんか?ありがとうございます。 – Hadee