亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

了解 Razor 數據持久性

了解 Razor 數據持久性

呼如林 2023-10-04 15:41:10
我目前正在學習剃刀頁面,我被這個問題困住了:我有一個自定義類,其中包含一組成員,其中包括其他自定義類的數組。一旦頁面開始,它們就會啟動。之后,用戶可以更改值或添加或刪除數組行。然而,一旦用戶確認更改,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();    }
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您應該將 OnGet 方法傳遞的數據保留在視圖中(如hidden controls),然后將它們傳遞給 OnPost 方法,而 OnPost 方法需要receive parameter從視圖中獲取 OnGet 方法中的數據。


我創建了一個簡單的演示供您參考:


? [BindProperty]

? ? public Employee Employee { get; set; }


? ? public void OnGet()

? ? {

? ? ? ? Employee = new Employee()

? ? ? ? {

? ? ? ? ? ? Id = 1,

? ? ? ? ? ? Name = "aaaa"

? ? ? ? };

? ? ? ? nExpenses = 1;

? ? }



? ? public void OnPost(Employee employee)

? ? {

? ? }

看法:


@page

@model WebApplication_core_razorpage.Pages.CodeSampleModel

@*

? ? For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

*@

<form method="post">

? ? @Html.HiddenFor(model => model.Employee.Id)

? ? @Html.HiddenFor(model => model.Employee.Name)

? ? <input id="Button1" type="submit" value="button" />

</form>

員工類別:


public class Employee

? ? {

? ? ? ? public int Id { get; set; }

? ? ? ? public string Name { get; set; }

? ? }


查看完整回答
反對 回復 2023-10-04
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號