populate the textbox from the value stored in the database table

sblb 1,166 Reputation points
2022-08-25T11:06:58.617+00:00

Hi,
I've a class model as below

public partial class Developer  
  
    {  
        public int Id { get; set; }     
        public int _currentYear = DateTime.Today.Year;       
        public string EnvT { get { return _currentYear.ToString(); } }    
}  

I want to retrieve EnvT in TextBox as below

<EditForm Model="@dev" OnValidSubmit="@OnValidSubmit">  
    <DataAnnotationsValidator />  
 <RadzenTextBox style="width: 100%;" Name="Numéro EnvT" @bind-Value=@dev.EnvT Disabled=true/>  
                        <ValidationMessage For="@(() => dev.EnvT)" />  
</EditForm>  

Edit razor page is

<FormEdit ButtonText="Update" dev="dev"  
      OnValidSubmit="@EditDeveloper" />  
  
@code {  
  
    [Parameter] public int developerId { get; set; }  
    Developer dev = new Developer();  
  
    protected async override Task OnParametersSetAsync()  
    {  
        dev = await http.GetFromJsonAsync<Developer>($"api/developer/{developerId}");  
  
    }  
  
    async Task EditDeveloper()  
    {  
        await http.PutAsJsonAsync("api/developer", dev);  
        await js.InvokeVoidAsync("alert", $"Updated Successfully!");  
        uriHelper.NavigateTo("developer");  
    }  

I've a message error : Property or indexer 'property' cannot be assigned to -- it is read only

I don't how why I've this error.
Do I have to do this to find the EnvT value in the database table?

Thanks in advance!

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,374 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,197 questions
{count} votes

2 answers

Sort by: Most helpful
  1. sblb 1,166 Reputation points
    2022-08-25T15:26:00.833+00:00

    If EnvT is a field in the database then your design makes little sense.

    Why?

    I want to fill EnvT, which is defined in database, when I create and edit the line from DataGrid (CRUD).

    0 comments No comments

  2. sblb 1,166 Reputation points
    2022-08-25T15:44:25.407+00:00

    It might seem to work today but next year EnvT will return 2023 while the table could contain 2022.

    It's exactly what I want; each line of my table will have 2022 for EnvT and when the Year Change the new line of the table will take 2023.
    Is there a problem with doing this?
    Do you have a counter example?