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.
Here's my 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
CodePudding user response:
you are trying to access the non-static member using the class name. To resolve this error you need to change the static member or create an instance of an 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; }
}
}
CodePudding user response:
<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.