Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4034

When changing value in an InputSelect in Blazor - it resets the value back to the previous selected value when submitting. How do I fix this?

$
0
0

OK I'm new to Blazor and I'm trying to change the value set to an InputSelect control. When I change the value, it resets it back to what it was before I changed it. Here is my code:

@page "/movies/edit"@rendermode InteractiveServer@using Microsoft.EntityFrameworkCore@using BlazorWebAppMovies.Models@using BlazorWebAppMovies.Data@inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory@inject NavigationManager NavigationManager<PageTitle>Edit</PageTitle><h1>Edit</h1><h2>Movie</h2><hr />@if (Movie is null){<p><em>Loading...</em></p>}else{<div class="row"><div class="col-md-4"><EditForm method="post" Model="Movie" OnValidSubmit="UpdateMovie" FormName="edit" Enhance><DataAnnotationsValidator /><ValidationSummary role="alert"/><input type="hidden" name="Movie.Id" value="@Movie.Id" /><div class="mb-3"><label for="title" class="form-label">Title:</label><InputText id="title" @bind-Value="Movie.Title" class="form-control" /><ValidationMessage For="() => Movie.Title" class="text-danger" /></div><div class="mb-3"><label for="releasedate" class="form-label">Release Date:</label><InputDate id="releasedate" @bind-Value="Movie.ReleaseDate" class="form-control" /><ValidationMessage For="() => Movie.ReleaseDate" class="text-danger" /></div><div class="mb-3"><label for="genre" class="form-label">Genre:</label><InputText id="genre" @bind-Value="Movie.Genre" class="form-control" /><ValidationMessage For="() => Movie.Genre" class="text-danger" /></div><div class="mb-3"><label for="price" class="form-label">Price:</label><InputNumber id="price" @bind-Value="Movie.Price" class="form-control" /><ValidationMessage For="() => Movie.Price" class="text-danger" /></div><div class="mb-3"><label for="rating" class="form-label">Rating:</label><InputText id="rating" @bind-Value="Movie.Rating" class="form-control" /><ValidationMessage For="() => Movie.Rating" class="text-danger" /></div><div class="mb-3"><label for="moviestarrating" class="form-label:">Movie Star Rating:</label><InputSelect id="moviestarrating" @bind-value="Movie.MovieStarRatingId" class="form-control">                        @foreach (var movieStarRating in StarRatings)                        {                            @if (movieStarRating.Id != 0)                            {                                if (movieStarRating.Id == Movie.MovieStarRatingId)                                {<option value="@movieStarRating.Id" selected>@movieStarRating.Descr</option>                                }                                else                                {<option value="@movieStarRating.Id">@movieStarRating.Descr</option>                                }                            }                                                   }</InputSelect><ValidationMessage For="() => Movie.MovieStarRating" class="text-danger"></ValidationMessage></div><button type="submit" class="btn btn-primary">Save</button></EditForm></div></div>}<div><a href="/movies">Back to List</a></div>@code {    private BlazorWebAppMoviesContext context = default!;    [SupplyParameterFromQuery]    private int Id { get; set; }    [SupplyParameterFromForm]    private Movie? Movie { get; set; }    private IList<MovieStarRating> StarRatings { get; set; } = [];    protected override async Task OnInitializedAsync()    {        using var context = DbFactory.CreateDbContext();        Movie ??= await context.Movie.FirstOrDefaultAsync(m => m.Id == Id);        if (Movie is null)        {            NavigationManager.NavigateTo("notfound");        }        StarRatings = await context.MovieStarRating.ToListAsync();    }    // To protect from overposting attacks, enable the specific properties you want to bind to.    // For more information, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.    private async Task UpdateMovie()    {        using var context = DbFactory.CreateDbContext();        context.Attach(Movie!).State = EntityState.Modified;        try        {            await context.SaveChangesAsync();        }        catch (DbUpdateConcurrencyException)        {            if (!MovieExists(Movie!.Id))            {                NavigationManager.NavigateTo("notfound");            }            else            {                throw;            }        }        NavigationManager.NavigateTo("/movies");    }    private bool MovieExists(int id)    {        using var context = DbFactory.CreateDbContext();        return context.Movie.Any(e => e.Id == id);    }}

I'm sure this is probably something simple but like I say, I'm new to Blazor here. I've been playing around with it some since I'm going to develop a new web app using it and I'm also trying to improve my skillset here.

I want to further emphasize what occurs - Suppose I change the Star Rating from 3 Stars to 4 Stars - I do that and I click the update button. What happens is for a split second is that the value in the drop down list changes BACK to 3 Stars and the value doesn't update in the database (well I guess it does but is the same value since it somehow goes back to that on the submit click).


Viewing all articles
Browse latest Browse all 4034

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>