watchlists/create:
Код: Выделить всё
@page "/watchlists/create"
@using Microsoft.EntityFrameworkCore
@using RookSecurities.com.Models
@using System.Security.Claims
@using Microsoft.AspNetCore.Authorization
@inject IDbContextFactory DbFactory
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider Auth
@attribute [Authorize]
Create
Create
Watchlist
Name:
Create
[url=/watchlists]Back to List[/url]
@code {
[SupplyParameterFromForm]
private Watchlist Watchlist { get; set; } = new();
// To protect from overposting attacks, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.
private async Task AddWatchlist()
{
var authState = await Auth.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity?.IsAuthenticated == true)
{
var userId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (userId != null)
{
Watchlist.UserId = userId; // Set here, NOT in form
await using var db = await DbFactory.CreateDbContextAsync();
db.Watchlist.Add(Watchlist);
await db.SaveChangesAsync();
Watchlist = new(); // Reset
}
}
}
}
Код: Выделить всё
using RookSecurities.com.Data;
using System.ComponentModel.DataAnnotations;
namespace RookSecurities.com.Models
{
public class Watchlist
{
[Required]
public int Id { get; set; }
[Required]
public string UserId { get; set; }
public string Name { get; set; }
public ApplicationUser User { get; set; } = null!;
public List Symbols { get; set; } = new();
}
}
InvalidOperationException: EditForm требуется либо параметр Model, либо параметр EditContext, укажите один из них.
Однако, глядя на код EditForm, кажется, что модель установлена:
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/798 ... odel-error
Мобильная версия