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).
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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!
Does it work if you change the definition to public string EnvT { get { return _currentYear.ToString(); } set { } } for researching purposes?
If I change model class as
public string EnvT { get { return _currentYear.ToString(); } set { } }
it's ok! That's return the valueEnvT
thanks!
I tried
private set{}
and it didn't work. Do you know why?The Private access modifier only allows methods within the class to modify the property.
Property Access control
Access modifiers are a fundamental C# feature.
Access Modifiers (C# Programming Guide)
This update can not be the actual fix since the get always returns DateTime.Today.Year.ToString(). The set logic uses an empty set while the property has a defined field. In other words, your design makes no logical sense. It might seem to work today but next year EnvT will return 2023 while the table could contain 2022.
Sign in to comment