Record with null values gets inserted in database automatically after editing existing record in Blazor

Viewed 32

I am using a MudBlazor table component with inline editing and I have all the functionality working except the part where if I perform an edit on a record, the record updates but in addition, a new row gets added with all 0 values. I am using the out of box weather forecast solution with a database that I made for it. Does anyone now what could be causing this issue?

FetchData.razor:

<MudTable  FixedHeader="true" Height="450px" Elevation="25" Items="GetWeatherForecast()" Filter="new Func<WeatherForecast, bool>(Search)" @bind-customer="forecast" CanCancelEdit="@canCancelEdit" 
          CommitEditTooltip="Commit Edit" OnCommitEditClick="EditWeather" Hover="@hover" Bordered="@bordered" Striped="@striped" HorizontalScrollbar="true"
          RowEditPreview="BackupItem" RowEditCancel="ResetItemToOriginalValues">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Forecasts</MudText>
        <br />
        <br />
        <div></div>
        <MudSpacer />  
    </ToolBarContent>
    <HeaderContent>
        <MudTh>Tempc</MudTh>
        <MudTh>Tempf</MudTh>
        <MudTh>Summary</MudTh>    
        <MudTh>Percentage</MudTh>    
    </HeaderContent>
    <RowTemplate>
        @{
             float percentage = (float)@context.TemperatureC / sumOfTemperatures * 100;
        }
        <MudTd DataLabel="TemperatureC">@context.TemperatureC</MudTd>
        <MudTd DataLabel="TemperatureF">@context.TemperatureF</MudTd>
        <MudTd DataLabel="Summary">@context.Summary</MudTd>
        <MudTd DataLabel="percentage">@percentage</MudTd>
    </RowTemplate>
        <RowEditingTemplate>
             <MudTd DataLabel="TemperatureC">
            <MudTextField @bind-Value="@context.TemperatureC" Required />
        </MudTd>
          @{
             float percentage = (float)@context.TemperatureC / sumOfTemperatures * 100;
        }
  <MudTd DataLabel="TempF">@context.TemperatureF</MudTd>
  <MudTd DataLabel="Summary">@context.Summary</MudTd>
      <MudTd DataLabel="percentage">@percentage</MudTd>
    </RowEditingTemplate>
</MudTable>

@code{
    private string searchString = "";
    private bool hover = true;
    private bool striped = true;
    private bool bordered = true;
    private bool ronly = false;
    private bool canCancelEdit = true;
    private bool blockSwitch = false;
    int tempc;
    private WeatherForecast elementBeforeEdit;
    bool isOpen;
    private int sumOfTemperatures;
    private WeatherForecast forecast = new WeatherForecast();
    private List<WeatherForecast> forecasts = new List<WeatherForecast>();
    protected override async Task OnInitializedAsync()
    {
        GetWeatherForecast();
        sumOfTemperatures = forecasts.Sum(f => f.TemperatureC);

    }
   private void Save()
    {
        weatherService.SaveWeatherForecast(forecast);
        forecast = new WeatherForecast();
        snackBar.Add("Forecast Saved.", Severity.Success);
        GetWeatherForecast();
        OnInitializedAsync();

    }
    private void EditWeather()
    {
        weatherService.EditWeatherForecast(forecast);
       
        snackBar.Add("Edited", Severity.Success);
        GetWeatherForecast();
        OnInitializedAsync();
    }

WeatherForecastService.cs:

   public void SaveWeatherForecast(WeatherForecast forecast)
        {
            if (forecast.Id == 0) _dbContext.Forecasts.Add(forecast);
            else _dbContext.Forecasts.Update(forecast);
            _dbContext.SaveChanges();
        }
        public void EditWeatherForecast(WeatherForecast forecast)
        {
            _dbContext.Forecasts.Update(forecast);
            _dbContext.SaveChanges();
        }
0 Answers
Related