I want to assign taskModel.Id to the predefined id value

Umut Can 0 Reputation points
2024-01-19T10:33:06.74+00:00

taskmodel.text value comes to the function correctly, but .id value always returns null 0

@page "/characters"

<h3>Characters</h3>



<EditForm Model="taskModel" OnValidSubmit="HandleSubmit">
    <DataAnnotationsValidator />
    <div>
        <InputNumber id="id" @bind="taskModel.Id" class="form-control" Value="5" hidden> @queryid</InputNumber>
        <ValidationMessage For="@(() => taskModel.Id)" />
            @taskModel.Id = 5
    </div>
    <div>
        <label for="name">Name</label>
        <InputText id="name" @bind-Value="taskModel.Text" class="form-control"></InputText>
        <ValidationMessage For="@(() => taskModel.Text)" />
    </div>

    <br />
    <button type="submit" class="btn btn-primary">Create Character</button>
   
    <ValidationSummary />
</EditForm>

<p style="height:500px;"></p>
@code {
    TaskModel taskModel= new TaskModel();
    public int queryid=5;    
    private void HandleSubmit()
    {
        string ad = taskModel.Text;
        int alla= taskModel.Id;
    }
}

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,664 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 29,781 Reputation points
    2024-01-19T11:33:44.69+00:00

    If I understand correctly you want to set the Id field. I think this is what you are after.

    @page "/characters"
    @using System.ComponentModel.DataAnnotations
    @inject ILogger<Character> Logger
    <h3>Characters</h3>
    
    
    <EditForm Model="taskModel" OnValidSubmit="HandleSubmit" FormName="RomulanAle">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <div>
            <InputNumber @bind-Value="taskModel.Id" class="form-control" />
        </div>
        <div>
            <label for="name">Name</label>
            <InputText @bind-Value="taskModel.Text" class="form-control" />
        </div>
    
        <br />
    
        <button type="submit" class="btn btn-primary">Create Character</button>
    </EditForm>
    
    @code {
        [SupplyParameterFromForm]
        public TaskModel? taskModel { get; set; }
    
        protected override async Task OnInitializedAsync()
        { 
            taskModel ??= new() { Id = 5 };
        }
        private void HandleSubmit()
        {
            Logger.LogInformation("alla = {alla} ad = {ad}", taskModel.Text, taskModel.Id);
        }
    
        public class TaskModel
        {
            [Required]
            public int? Id { get; set; }
            [Required]
            public string? Text { get; set; }
        }
    }
    

    The official documentation illustrates how to do handle binding forms in Blazor. ASP.NET Core Blazor forms binding


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.