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

Blazor default scaffolded CRUD create page not finding model

$
0
0

I created several CRUD pages based on my models, using the create scafolded item as per the Blazor tutorial. For some reason I can't figure out, some of them are working and some of them don't.

The code here is nearly identical to a similar create page I have for another model. The SQL database has the table. Even the Edit form works perfectly, after I added an entry to the SQL database via query. But the Create page throws this error:

InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.

I tried explicitly creating an instance of the model in the initialize function, but the binding would not work. I am completely at a loss.

@page "/characters/create"@using Microsoft.EntityFrameworkCore@using App.src.Models@inject IDbContextFactory<SciFiLarpApp.Data.AppContext> DbFactory@inject NavigationManager NavigationManager<PageTitle>Create</PageTitle><h1>Create</h1><h2>Ship</h2><hr /><div class="row"><div class="col-md-4"><EditForm method="post" Model="Ship" OnValidSubmit="AddShip" FormName="create" Enhance><DataAnnotationsValidator /><ValidationSummary class="text-danger" /><div class="mb-3"><label for="name" class="form-label">Name:</label> <InputText id="name" @bind-Value="Ship.Name" class="form-control" /> <ValidationMessage For="() => Ship.Name" class="text-danger" /> </div>        <div class="mb-3"><label for="captainid" class="form-label">CaptainID:</label> <InputNumber id="captainid" @bind-Value="Ship.CaptainID" class="form-control" /> <ValidationMessage For="() => Ship.CaptainID" class="text-danger" /> </div>        <button type="submit" class="btn btn-primary">Create</button></EditForm></div></div><div><a href="/ships">Back to List</a></div>@code {    [SupplyParameterFromForm]    private Ship Ship { get; set; } = new();    private async Task AddShip()    {        using var context = DbFactory.CreateDbContext();        context.Ship.Add(Ship);        await context.SaveChangesAsync();        NavigationManager.NavigateTo("/ships");    }}

And here's my model:

namespace App.src.Models{    public class Ship    {        public int Id { get; set; }        public string? Name { get; set; }        public int CaptainID { get; set; }        public Character Captain { get; set; } = null!;    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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