嗨,我正在尝试创建一个与我的模型相对应的输入字段,如果所需字段为null,则它会向您显示填充字段的信息,但会导致错误。
这是我的型号:
public class ToDoItem
{
public DateTime? DueDate { get; set; }
[Required]
public string? ToDo { get; set; }
}
这是我的输入字段
<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>
这是我的错误
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:KerjaMencobaSimpleDemoSimpleDemoPagesToDo.razor 15
<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();
...
}
这里CCD_;对象引用";(例如(错误告诉你是需要的。
您正试图使用类名访问非静态成员。若要解决此错误,您需要更改静态成员或创建对象的实例。
@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; }
}
}