简体   繁体   English

为什么我的 blazor 的 @bind-value 返回错误?

[英]Why does my @bind-value of my blazor returns an error?

Hi I'm trying to create an Input Field that correspond to my Model in which if the required field is null then it will show you that to fill the field but it results in an error.嗨,我正在尝试创建一个与我的 Model 对应的输入字段,其中如果必填字段是 null,那么它会告诉您填写该字段,但会导致错误。

Here's my model:这是我的 model:

public class ToDoItem
{
    public DateTime? DueDate { get; set; }

    [Required]
    public string? ToDo { get; set; }
}

Here's my input field这是我的输入字段

<input id = "txtToDo" @bind-value = "@ToDoItem.ToDo" placeholder = "What You Need To Do" />
<input type = "datetime-local" @bind = "DueDate" placeholder = "Due Date" />
<button @onclick = "Save">Save</button>

Here's my Error这是我的错误

Severity    Code    Description Project File    Line    Suppression State
Error (active)  CS0120  An object reference is required for the non-static field, method, or property 'ToDoItem.ToDo'       e:\Kerja\Mencoba\SimpleDemo\SimpleDemo\Pages\ToDo.razor 15  

you are trying to access the non-static member using the class name.您正在尝试使用 class 名称访问非静态成员。 To resolve this error you need to change the static member or create an instance of an object.要解决此错误,您需要更改 static 成员或创建 object 的实例。

 @using System.ComponentModel.DataAnnotations
<input id = "txtToDo" @bind-value = "@ToDoItem.ToDo" placeholder = "What You Need To Do" />
<input type = "datetime-local" @bind = "@ToDoItem.DueDate" placeholder = "Due Date" />
<button @onclick = "Save">Save</button>
@code{

    private void Save(){

    }
    
    public class ToDoItem
    {
      public static DateTime? DueDate { get; set; } 

     [Required]
     public static string? ToDo { get; set; }
    }

}
<EditForm Model="item">
  <input id = "txtToDo" @bind-value = "@item.ToDo" placeholder = "What You Need To Do" />
  <button @onclick = "Save">Save</button>
</EditForm>

@code
{
   ToDoItem item = new();

   ...
}

Here, item is the "object reference" (instance) that the error tells you is needed.在这里, item是错误告诉您需要的“对象引用”(实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM