Passing ViewModel with Dictionary properties to View throws NullReferenceException

Emmanuel Campos 0 Reputation points
2024-09-24T03:31:58.87+00:00

I have a ViewModel that includes two Dictionary properties, Genres and Producers, which I want to send to the View. However, when I try to loop through the Dictionary properties in the View, I get a NullReferenceException error. The data appears to be passing normally until that point. How can I fix this issue?

public class SeriesViewModel : GenericViewModel
{
    [Required(ErrorMessage = "A video link must be specified")]
    public string VideoLink { get; set; }
    [Required(ErrorMessage = "A producer must be selected")]
    public int ProducerId { get; set; }
    [Required(ErrorMessage = "At least 1 genre must be selected")]
    public int PrimaryGenreId { get; set; }
    public int? SecondaryGenreId { get; set; }

    // Navigation properties
    public string ProducerName { get; set; }
    public string PrimaryGenreName { get; set; }
    public string SecondaryGenreName { get; set; }

    // When I try to initialize the Genres property with "new Dictionary<int, string>()" the ViewModel brings the Genres list empty
    public Dictionary<int, string> Producers { get; set; }
    public Dictionary<int, string> Genres { get; set; }
}
@model Application.ViewModels.SeriesViewModels.SeriesViewModel

<h1>@Model.Producers[24]</h1>

<div class="mb-3">
    <label asp-for="ProducerId" class="form-label">Producer</label>
    <select asp-for="ProducerId" class="form-select">
        <option>Select a producer</option>
        @foreach (var producer in Model.Producers)
        {
            <option value="@producer.Key">@producer.Value</option>
        }    
    </select>
    <span asp-validation-for="ProducerId" class="text-danger"></span>
</div>

<div class="mb-3">
    <label asp-for="PrimaryGenreId" class="form-label">Primary Genre</label>
    <select asp-for="PrimaryGenreId" class="form-select">
        <option>Select the first genre</option>
        @foreach (var genre in Model.Genres)
        {
            <option value="@genre.Key">@genre.Value</option>
        }
    </select>
    <span asp-validation-for="PrimaryGenreId" class="text-danger"></span>
</div>

<div class="mb-3">
    <label asp-for="SecondaryGenreId" class="form-label">Secondary Genre</label>
    <select asp-for="SecondaryGenreId" class="form-select">
        <option>Select the secondary genre</option>
        @foreach (var genre in Model.Genres)
        {
            <option value="@genre.Key">@genre.Value</option>
        }
    </select>
    <span asp-validation-for="SecondaryGenreId" class="text-danger"></span>
</div>
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
740 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,474 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.