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

Variable display issue in Razor page in Blazor App

$
0
0

I'm learning Blazor. On my page the contents of the _persons list just flashes by when I click the Search button, is this variable being garbage collected? Thanks in advance.

@page "/"@using BlazorPorter.Models@using BlazorPorter.Context@using Microsoft.EntityFrameworkCore@inject SqliteContext SqliteContext@rendermode InteractiveServer<PageTitle>Home</PageTitle><form class="form-control"><input placeholder="id" @bind="_id"/><input placeholder="name" @bind="_name"/><button class="btn-primary" @onclick="@Insert">Insert</button></form><form class="form-control"><button class="btn-primary" @onclick="Search">Search</button></form>@if (_persons != null){    @foreach (Person p in _persons)    {<p>@p.Id</p><p>@p.Name</p>    }}@code{    private string? _id;    private string? _name;    private List<Person>? _persons;    private async Task Insert()    {        if (_id != null && _name != null)        {            await SqliteContext.AddAsync(new Person() { Id = _id, Name = _name });            await SqliteContext.SaveChangesAsync();        }    }    private void Search()    {        if (SqliteContext.Persons != null)        {            _persons = SqliteContext.Persons.ToList();        }    }}

At first I thought the SqliteContext wasn't querying the content because it wasn't showing up on the page. But after I debugged it using breakpoints, I realized it was there. On successive clicks of the Search button, the content of the _persons list also flashes.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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