TimTheCoder 16 Newbie Poster

Good morning everyone, I have an MVC projet in ASP.NET and I got a little problem with the project, when I run the main program.cs script it breaks and says that the Model is null. I have followed every required step when implementing an entity relationship framework in ASP.NET. I suspect my problem is in the AlienController.cs file. Here is how am building the app

namespace L06HandsOn.Models
{
   //alien model
    public class Alien
    {

        public int? arms {get;set;}
        public int? legs { get; set; }
        public int? heads { get; set; } 
        public DateTime? BirthDate { get; set; }
        public Planet PlanetOrigin { get; set; }    

    }
    public enum Planet
    {
        Mercury,Venus, Earth, WhatOnceWas,Jupiter,Saturn,Uranus,Neptune,TheUnappreciatedPluto
    }
}

and below is my datacontext file

using Microsoft.EntityFrameworkCore;

namespace L06HandsOn.Models
{
    public class AliensContext:DbContext
    {
        public AliensContext(DbContextOptions<AliensContext> options) : base(options)
        {

        }
        public DbSet<Alien> Aliens { get; set; }
    }
}

and below is my aliencontroller.cs file

using L06HandsOn.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace L06HandsOn.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AlienController:Controller
    {
        private readonly AliensContext _context;

        //require the parameter on constructor 
        public AlienController(AliensContext cont)
        {
            _context =cont; 
        }
        [HttpGet]
        //get the aliens
        public async Task<IActionResult> Index()
        {
            return View(await _context.Aliens.ToListAsync());
        }
        [HttpGet]
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            var alien=await _context.Aliens.SingleOrDefaultAsync(); 
            if(alien== null)
            {
                return NotFound();
            }
            return View(alien);
        }
    }
}

and finally here is how am registering the database context in the main of the program

using L06HandsOn.Models;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

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

var connectionString = "Data Source=AliensEntityFramework.db";
builder.Services.AddDbContext<AliensContext>(optionsAction=>optionsAction.UseSqlite(connectionString));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/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.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Why is my ASP.NET app returning a null for the Model please help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.