我目前正在学习剃刀页面,我被这个问题困住了:
我有一个自定义类,其中包含一组成员,其中包括其他自定义类的数组。
一旦页面开始,它们就会启动。之后,用户可以更改值或添加或删除数组行。
然而,一旦用户确认更改,OnPost 就会抛出异常,就好像数组从未启动过一样。
解决这个问题的最佳方法是什么?
代码示例:
[BindProperty(SupportsGet = true)]
public long UID { get; set; }
[BindProperty(SupportsGet = true)]
public int UDEP { get; set; }
[BindProperty]
public int nExpenses { get; set; }
// Custom object
[BindProperty]
public Employee Employee { get; set; }
public void OnGet()
{
Employee = new Employee(UID, UDEP);
// Loading arrays data from a Database
Employee = Employee.ExtractPersonalData(UID);
Employee.Expenses.ExtractSheet(Month, Year, Employee.ID);
Employee.Expenses.ExtractBoard();
Employee.Expenses.ExtractProjects(Month, Employee.ID);
// There are 1 boards and 4 projects with 30 decimal[] each
nExpenses = Employee.Expenses.Boards.Count > 0 ? Employee.Expenses.Boards[0].Projects.Count : 0;
}
public IActionResult OnPost()
{
// All variables within Employee equal NULL
if(nExpenses < Employee.Expenses.Boards[0].Projects.Count)
DeleteRow();
else if (nExpenses > Employee.Expenses.Boards[0].Projects.Count)
AddRows();
Employee.Expenses.SaveSheet();
}
回首忆惘然
相关分类