1

Index.cshtml is nothing but this, which Visual Studio created for me:

@model TestModel
@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

Trying to view the page gives me an unhandled exception error, and the error is NullReferenceException: Object reference not set to an instance of an object.

I have spent a decent amount of time on Google and even searching StackOverflow for answers, but I can't find anything that addresses exactly this problem. Can anyone help?

Edit: Index.cshtml is located in /Views/Text/Index.cshtml

Text of the controller is:

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

namespace MvcMovie.Controllers
{
    public class TestController : Controller
    {
        [BindProperty]
        public string FormInput { get; set; }
        [BindProperty]
        public string Firstname { get; set; }
        [BindProperty]
        public string Lastname { get; set; }

        public IActionResult Index()
        {
            ViewData["output"] = Firstname + "<br>" + Lastname;
            return View();
        }
    }
}

I've also added @page to the top of the cshtml file. I still get a NullReferenceException at ViewData["Title"] = "Index";. Alexander I read your reply on the other thread and there's not enough context for me to understand it. I am just getting started with MVC and Razor pages having taken over a project that uses both, so it's very possible that I am confusing and/or mixing the two. I'll try and read those docs you linked to when I have the chance.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
Mike Payne
  • 87
  • 2
  • 10
  • 1
    What does the controller look like? – Matt U Dec 03 '19 at 22:13
  • can u show the coded for controller please – harsha reddy Dec 04 '19 at 07:05
  • See my answer [here](https://stackoverflow.com/questions/54813316/object-reference-not-set-to-an-instance-of-an-object-viewdatatitle-index/54817448#54817448) – Alexander Dec 04 '19 at 08:10
  • I edited the post above to show the text of the controller and a bit more explanation. – Mike Payne Dec 04 '19 at 13:16
  • I have tried, but I cannot reproduce the issue. There must be something else, which causes it. Can you provide more details? For example the stack trace of the exception? Or, is there something special in your Startup.cs code? What is the output to the browser window after letting the app to run after exception? – Zoka Dec 04 '19 at 14:08
  • Do you mix using MVC and Razor Pages?You'd better not do that if you have mixed them.Could you share steps to reproduce your problem with a new project? – Ryan Dec 06 '19 at 07:33

0 Answers0