簡體   English   中英

ASP.NET Core Razor Page 為列表創建新頁面總是給出 System.NullReferenceException

[英]ASP.NET Core Razor Page create new for list always give System.NullReferenceException

當我打開創建新項目時,打開此頁面輸入保存到列表的值,但我總是在項目內得到空值,我顯示為下拉列表的項目組也給出空值錯誤

鑒於

<input asp-for="@Model.Items.Id" />
<label asp-for="@Model.Items.Groups.Id">Group nme</label>
        <select asp-for="@Model.Items.Groups.Id"  asp-items="@(new SelectList(Model.AllGroup,"Id","GroupName" ))"></select>    

在 CS 文件中

    public class Items_Class
    {
        public int Id  { get; set; }
        public string ItemName { get; set; }
        public decimal ItemPrice { get; set; }
        public decimal ItemQuantity { get; set; }
        public Groups_Class Groups { get; set; }
    }


    public class CreateModel : PageModel
    {
       private readonly Groups_Methods g_Mrthod;

       [BindProperty]
       public Items_Class Items { get; set; }
       public List<Groups_Class> AllGroup { get; set; }
       
       public CreateModel(Groups_Methods  G_Mrthod)
       {
            g_Mrthod = G_Mrthod;
          
       }    
    

       public void OnGet()
       {
           AllGroup = g_Mrthod.GetAllGroups();        
       }
   } 

我很難理解你的問題。 步驟是什么?

  • 加載頁面
  • 添加新項目
  • 節省
  • 服務器端的ItemName, ItemPrice 和 ItemQuantity 有空值 如果是這些步驟,請考慮添加一個

public void OnPost()並閱讀https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-6.0&tabs=visual-studio#write-a-basic-form

您可以嘗試設置您的ItemsAllGroup

[BindProperty]
        public Items_Class Items { get; set; } = new Items_Class();
        public List<Groups_Class> AllGroup { get; set; } = new List<Groups_Class>();

如果它仍然不起作用,請嘗試檢查g_Mrthod.GetAllGroups(); 一片空白。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM