Aspnetcore: [Blazor] Context doesn't exist

Created on 24 Jun 2019  路  3Comments  路  Source: dotnet/aspnetcore

Hi guys !

I'm comming to you because i'm facing an issue on Blazor.

CS0103 The name 'context' does not exist in the current context

I have 3 files (It's little bit like MVVM blazor

CTableViewModel.cs :

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BlazorWebApp.Client.Helpers;
using Microsoft.AspNetCore.Components;

namespace BlazorWebApp.Client.ViewModels.Shared.Components.Tables
{
    public class CTableViewModel<TItem> : ComponentBaseExtensions
    {
        [Parameter] protected RenderFragment HeaderTemplate { get; set; }
        [Parameter] protected RenderFragment<TItem> RowTemplate { get; set; }
        [Parameter] protected string NoItem { get; set; }
        [Parameter] protected Func<Task<List<TItem>>> RefreshItems { get; set; }

        protected List<TItem> Items = new List<TItem>();

        protected override async Task OnParametersSetAsync()
        {
            await base.OnParametersSetAsync();
            await OnRefreshItems();
        }

        private async Task OnRefreshItems()
        {
            SetIsLoading(true);
            Items = await RefreshItems() ?? new List<TItem>();
            SetIsLoading(false);
        }
    }
}

CTableView.razor :

@typeparam TItem
@inherits CTableViewModel<TItem>

<div class="table-responsive">
    <table class="table table-hover">
        <thead>
        <tr>
            @if (HeaderTemplate != null)
            {
                @HeaderTemplate
            }
        </tr>
        </thead>
        <tbody>
        @foreach (TItem item in Items)
        {
            <tr>
                @RowTemplate(item)
            </tr>
        }
        </tbody>
    </table>
</div>

And finally Index.razor :

@layout MainLayout
@page "/"
@inherits BlazorWebApp.Client.ViewModels.Pages.Index.IndexViewModel

<CTableView NoItem="Nothing" RefreshItems="@(async () => await GetUsers())" Items="UserAuth">
    <HeaderTemplate>
        <th class="text-center">Mail</th>
        <th class="text-center">Password</th>
    </HeaderTemplate>
    <RowTemplate>
        <td class="text-center">@context.Email</td>
        <td class="text-center">@context.Password</td>
    </RowTemplate>
</CTableView>

I got this issue since i updated to blazor preview 6.
Do you know why it's happening ?

Dotnet Version : 3.0.100-preview6-012264

Thank you for your kind help !

area-blazor question

Most helpful comment

Hi, thank you for your kind help ! 馃挴

I found the issue, i had to add in the file _Imports.razor :

@* Components *@
@using BlazorWebApp.Client.Views.Shared.Components.Tables

@* Components Models *@
@using BlazorWebApp.Client.ViewModels.Shared.Components.Tables

The funny thing is that the error about the context doens't really makes lot of senses. A compilation error about component not found of something would be more clear in my opinion !

@julienGrd I don't need to add it, it works !

All 3 comments

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.

You have to explicitely declare the context into a variable, like this (you can name context with a better name if you want)
<RowTemplate Context="context"> <td class="text-center">@context.Email</td> <td class="text-center">@context.Password</td> </RowTemplate>

good luck !

Hi, thank you for your kind help ! 馃挴

I found the issue, i had to add in the file _Imports.razor :

@* Components *@
@using BlazorWebApp.Client.Views.Shared.Components.Tables

@* Components Models *@
@using BlazorWebApp.Client.ViewModels.Shared.Components.Tables

The funny thing is that the error about the context doens't really makes lot of senses. A compilation error about component not found of something would be more clear in my opinion !

@julienGrd I don't need to add it, it works !

Was this page helpful?
0 / 5 - 0 ratings