ASP.NET Core 8.0 Razor – NullReferenceException when looping through Model

I am trying to build a simple app that iterates through ‘Car’ (model) names.

I am getting the following error when trying to iterate through items called through an API.

Index.cshtml:

@page
@model CarPageModel

<div class="text-center">
    <h1 class="display-4">Cars</h1>
    <hr/>
    @foreach (var c in @Model.Cars) {
    <span>@c.Name</span>
    } 
</div>

Index.cshtml.cs

Note: The API call works and returns data when entering debug mode.

using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CarApp.Pages;

public class CarPageModel : PageModel
{
    private readonly ILogger<CarPageModel> _logger;

    public CarPageModel(ILogger<CarPageModel> logger)
    {
        _logger = logger;
    }

    // Car is a Model
    public List<Car> Cars { get; set; } = default!;

    public async void OnGetAsync()
    {
        using(var httpClient = new HttpClient()) {

            // Call controller api
            var uri = new Uri("http://localhost:5009/api/cars");

            using var response = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync();

            var serializerOptions = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            }; 
            var data = await JsonSerializer.DeserializeAsync<List<Car>>(stream, serializerOptions);
            Cars = data == null ? [] : data;
        }
    }

}

Program.cs

using Microsoft.EntityFrameworkCore;
using CarApp;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

// Add appsettings.json
builder.Configuration.AddJsonFile("appsettings.json",
        optional: true,
        reloadOnChange: true);

var connectionString = builder.Configuration.GetConnectionString("AppDatabaseCS");

builder.Services.AddDbContext<CarContext>(options =>
    options.UseSqlServer(connectionString));

builder.Services.AddControllers();
builder.Services.AddHttpClient();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.MapControllers();

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

I can provide additional code if needed. I just didn’t think the controller / model class was relevant but I can provide if needed.


Note: I took a look at similar questions and they are saying to add ‘return View()’ but I don’t know where to inject that code in my solution. But I don’t know if that is relevant to my issue.

1

The error is telling you that Cars is null. And the code initializes it to null:

public List<Car> Cars { get; set; } = default!;

Instead, initialize it to an empty list:

public List<Car> Cars { get; set; } = new List<Car>;

That way the loop will just silently iterate over 0 records.


Additionally, this is almost certainly wrong:

public async void OnGetAsync()

Using void makes this method un-awaitable. So if the expectation is that the framework will be able to wait until Cars is populated before continuing, this may prevent that. Return a Task instead:

public async Task OnGetAsync()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật