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

Trouble Binding InputSelect (drop down list) in Blazor from Database

$
0
0

I'm new to Blazor and the next issue I'm having is trying to bind dynamic data from a lookup table onto an InputSelect deal

Here's the Create Code:

@page "/movies/create"@rendermode InteractiveServer@using Microsoft.EntityFrameworkCore@using BlazorWebAppMovies.Models@using BlazorWebAppMovies.Data@inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory@inject NavigationManager NavigationManager<PageTitle>Create</PageTitle><h1>Create</h1><h2>Movie</h2><hr /><div class="row"><div class="col-md-4"><EditForm method="post" Model="Movie" OnValidSubmit="AddMovie" FormName="create" Enhance><DataAnnotationsValidator /><ValidationSummary class="text-danger" role="alert"/><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"><option value="">-- Select --</option>                    @foreach (var movieStarRating in StarRatings)                    {<option value="@movieStarRating.Id">@movieStarRating.Descr</option>                    }</InputSelect><ValidationMessage For="() => Movie.MovieStarRating" class="text-danger"></ValidationMessage></div><button type="submit" class="btn btn-primary">Create</button></EditForm></div></div><div><a href="/movies">Back to List</a></div>@code {    private BlazorWebAppMoviesContext context = default!;    [SupplyParameterFromForm]    private Movie Movie { get; set; } = new();    private IQueryable<MovieStarRating> StarRatings => context.MovieStarRating;    // To protect from overposting attacks, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.    private async Task AddMovie()    {        using var context = DbFactory.CreateDbContext();        context.Movie.Add(Movie);        await context.SaveChangesAsync();        NavigationManager.NavigateTo("/movies");    }}

It's throwing an "object reference not set to instance of object" or something like that saying that my context object is null. Again, I'm quite new to this.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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