Wenn Sie den Wert in einem InputSelect in Blazor ändern, setzt er den Wert beim Senden auf den vorherigen ausgewählten W

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wenn Sie den Wert in einem InputSelect in Blazor ändern, setzt er den Wert beim Senden auf den vorherigen ausgewählten W

by Anonymous » 05 Mar 2025, 05:19

Ok, ich bin neu in Blazor und versuche, den Wert auf eine Eingabe -Select -Steuerung zu ändern. Wenn ich den Wert ändere, setzt es ihn wieder auf das zurück, was er war, bevor ich ihn geändert habe. Hier ist mein Code: < /p>
@page "/movies/edit"
@rendermode InteractiveServer
@using Microsoft.EntityFrameworkCore
@using BlazorWebAppMovies.Models
@using BlazorWebAppMovies.Data
@inject IDbContextFactory DbFactory
@inject NavigationManager NavigationManager

Edit

Edit

Movie

@if (Movie is null)
{
Loading...
}
else
{







Title:



Release Date:



Genre:



Price:



Rating:



Movie Star Rating:

@foreach (var movieStarRating in StarRatings)
{
@if (movieStarRating.Id != 0)
{
if (movieStarRating.Id == Movie.MovieStarRatingId)
{
@movieStarRating.Descr
}
else
{
@movieStarRating.Descr
}
}
}



Save



}

Back to List

@code {
private BlazorWebAppMoviesContext context = default!;

[SupplyParameterFromQuery]
private int Id { get; set; }

[SupplyParameterFromForm]
private Movie? Movie { get; set; }

private IList 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 ... ng-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);
}
}

< /code>
Ich bin mir sicher, dass dies wahrscheinlich etwas Einfaches ist, aber wie ich sage, ich bin hier neu bei Blazor. Ich habe einige damit herumgespielt, seit ich eine neue Web -App nutze, und versuche auch, meine Fähigkeiten hier zu verbessern.

Top