C# random behaviour in a CRUD edit page.

sblb 1,166 Reputation points
2022-10-14T14:09:47.947+00:00

Hi,
My application has a strange behaviour for a function I put with the help of @AgaveJoe . Definition link to generate a variable ECO [WebApiSqlite][1]

Normally the ECO variable is generated when I validate the checkboxValue1 and then I validate the Edit page.

Now the ECO variable is generated without validating the checkboxValue1 variable. The variable ECO is generated when I validate the Edit page without the checkboxValue1 variable being validated.

Edit.razor

<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 http.GetAsync($"api/developer/SelectEcoById/{developerId}");   
       await js.InvokeVoidAsync("alert", $"Updated Successfully!");  
       uriHelper.NavigateTo("developer");          
    }          

}  

as you can see on picture below ECOSelected is false.
![250593-image.png][3]

the uriHelper.NavigateTo("developer");

The fetchData .razor is active to update and fetch the data and ECOSelected become true.
250518-image.png

How to avoid what the variable ECOSelected stay false when checkboxValue is false? and
when if checkboxValue1 = true then ECOSelected = true?

ECOSelected ECO variable is defined in model class

public bool ECOSelected { get; set; }  
        [NotMapped]  
        public string ECO   
        {  
            get  
            {  
                if (ECOSelected)  
                {  
                    return $"{ECOCount.ToString().PadLeft(3, '0')}/{ECOYear}";  
                }  

                return string.Empty;  
            }  
            set { }  
        }  

The controller

  [HttpGet("SelectEcoById/{id}")]  
         public async Task<ActionResult<Developer>> SelectEcoById(int id)  
        {  
            var developer = await _context.Developers.FindAsync(id);  
            if (developer == null)  
            {  
                return NotFound();  
            }  
            if (!developer.ECOSelected)  
            {  
               var values = await GenerateEcoByIdAsync(id);  

                    developer.ECOYear = values.Year;  
                    developer.ECOCount = values.Count;  
                    developer.ECOSelected = true;  


                await _context.SaveChangesAsync();  
            }  

            return developer;  
        }  

[1]: https://github.com/mjgebhard/WebApiSqlite+ [3]: /api/attachments/250593-image.png?platform=QnA

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,399 questions
0 comments No comments
{count} votes

11 answers

Sort by: Most helpful
  1. sblb 1,166 Reputation points
    2022-10-19T15:08:10.33+00:00

    I think that I found how I can solve my problem by modified GenearteEcoById to add one condition

    if(developer.ECOYear == 0 && developer.ECOCount == 0) && developer.ECOSelected==true)

    What do you think about it?