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

How do I setup a Blazor QuickGrid to recognize the fields in my class?

$
0
0

First of all, I'm completely new to Blazor so go easy on me. I've been doing C#, ASP.NET and Winforms programming for years, however. I'm taking an older Winforms, C#, .NET application and attempting to rewrite it in .NET 9.0, C#, and Blazor.

I've set up my class, created a DbContext, and added a QuickGrid to my .razor page. I've been using these tutorials as part of creating the Blazor page and connecting the app to the database:

https://learn.microsoft.com/en-us/dotnet/aspire/database/sql-server-integrations

https://learn.microsoft.com/en-us/aspnet/core/blazor/tutorials/movie-database-app/part-2?view=aspnetcore-9.0&pivots=vs

On my page Index.razor, I'm getting an error

InboundOriginalFileName does not exist in the current context

on the line where I define a property for the QuickGrid. The field InboundOriginalFileName is part of the class EncounterValidationOutput and is in the SQL results for the list object encountersValidationOutputs which is assigned to the Items property in the QuickGrid. I asked on Copilot and only got back the answer that my field must be defined in my class, which it is.

Why is the QuickGrid not recognizing that field? What can I do to fix this?

Index.razor:

@page "/encountervalidationoutputs"@using Microsoft.EntityFrameworkCore@using Microsoft.EntityFrameworkCore.Infrastructure@using Microsoft.AspNetCore.Components.QuickGrid@using EncounterBlazorApp.Models@using EncounterBlazorApp.Data@implements IAsyncDisposable@inject IDbContextFactory<EncounterBlazorAppContext> DbFactory<PageTitle>Index</PageTitle><h1>Index</h1><p><a href="encountervalidationoutputs/create">Create New</a></p><QuickGrid Class="table" Items="encounterValidationOutputs.AsQueryable()" TGridItem="EncounterValidationOutput"><PropertyColumn Property="InboundOriginalFileName" Title="InboundOriginalFileName" /></QuickGrid>@code {    private List<EncounterValidationOutput> encounterValidationOutputs = new();    private EncounterBlazorAppContext? context = null;    protected override async Task OnInitializedAsync()    {        context = await DbFactory.CreateDbContextAsync();         encounterValidationOutputs = await context.Set<EncounterValidationOutput>().FromSqlRaw(@" SELECT [Transaction_File_Name] AS InboundOriginalFileName FROM [mydatabase].[dbo].[repository]").ToListAsync();        // Debugging output         Console.WriteLine($"Fetched {encounterValidationOutputs.Count} items."); if (encounterValidationOutputs.Any()) { Console.WriteLine($"First item's InboundOriginalFileName: {encounterValidationOutputs.First().InboundOriginalFileName}"); } }    public async ValueTask DisposeAsync()    {        if (context != null)        {            await context.DisposeAsync();        }    }}

EncounterBlazorAppContext.cs

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.EntityFrameworkCore;using EncounterBlazorApp.Models;namespace EncounterBlazorApp.Data{    public class EncounterBlazorAppContext : DbContext    {        public EncounterBlazorAppContext(DbContextOptions<EncounterBlazorAppContext> options) : base(options)        { }        public DbSet<EncounterValidationOutput> EncounterValidationOutputs => Set<EncounterValidationOutput>();        protected override void OnModelCreating(ModelBuilder modelBuilder)        {            base.OnModelCreating(modelBuilder);            // Configure EncounterValidationOutput as a keyless entity            modelBuilder.Entity<EncounterValidationOutput>().HasNoKey();        }    }}

Program.cs

using EncounterBlazorApp.Components;using EncounterBlazorApp.Data;using Microsoft.EntityFrameworkCore;var builder = WebApplication.CreateBuilder(args);// Register IDbContextFactory for EncounterBlazorAppContextbuilder.Services.AddDbContextFactory<EncounterBlazorAppContext>(options =>    options.UseSqlServer(builder.Configuration.GetConnectionString("EncounterBlazorAppContext"), sqlOptions =>    {        sqlOptions.CommandTimeout(60); // Set your desired command timeout in seconds    }));// Add other servicesbuilder.Services.AddQuickGridEntityFrameworkAdapter();builder.Services.AddDatabaseDeveloperPageExceptionFilter();builder.Services.AddRazorComponents().AddInteractiveServerComponents();var app = builder.Build();// Configure the HTTP request pipelineif (!app.Environment.IsDevelopment()){    app.UseExceptionHandler("/Error", createScopeForErrors: true);    app.UseHsts();    app.UseMigrationsEndPoint();}app.UseHttpsRedirection();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>().AddInteractiveServerRenderMode();app.Run();

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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