0

I have a very simple starter .NET application with this method in my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SampleMVC.Models;

namespace SampleMVC
{
    public class TurtleController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Detail( int id ) {
            var turtle = new TurtleModel{ id = 1, title = "Pink Turtle" };
            return View("Detail",turtle);
        }
    }

My TurtleModel looks like this:

using System;

namespace SampleMVC.Models
{
    public class TurtleModel
    {
        public int id { get; set; }
        public string title { get; set; }
    }
}

and my view looks like this:

@page
@model SampleMVC.Models.TurtleModel;
@{
    <p>tester @Model.title</p>
}

Startup.cs (Routes)

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

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

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
}

However I'm getting a Null pointer / Object reference error when trying to view the URL /Turtle/Detail/1. Wondering if you can point me in the right direction? I have debugged the Controller and the turtleModel is definitely instantiated, so I don't see why the view thinks it is not after it has been passed down to it.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Squiggs.
  • 3,037
  • 6
  • 40
  • 73
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Richardissimo Jul 29 '18 at 06:07
  • Have you checked adding debug point in Detail action? Your code seems correct. But it seems that it might executing any other action. – Karan Jul 29 '18 at 06:24
  • @Karan - I've added a breakpoint and its definitely getting hit. This is a brand new SampleMVC application so no changes to default routes or anything. Without the Model.title in the view, the detail page is returned successfully to the controller is finding the view. – Squiggs. Jul 29 '18 at 06:26
  • how does your start up routes config look like? – McKabue Jul 29 '18 at 06:27
  • @McKabue - startup routes added. – Squiggs. Jul 29 '18 at 06:32

1 Answers1

0

The problem appeared to be in the view:

@page
@model SampleMVC.Models.TurtleModel;
@{
    <p>tester @Model.title</p>
}

Changed to

@model SampleMVC.Models.TurtleModel;
@{
    <p>tester @Model.title</p>
}
Squiggs.
  • 3,037
  • 6
  • 40
  • 73
  • 1
    You was using @page directive, which is specific for razor pages and brings different request handling (https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-2.1&tabs=visual-studio). – Dimitar Jul 29 '18 at 06:48