1

Hello to everybody at Stack Overflow and please pardon me if I am missing anything here!

I am studying ASP.NET to prepare myself for an internship. To this end, I am following along with a Microsoft Virtual Academy course entitled "Introduction to ASP.NET Core with Visual Studio 2017".

Specifically, I am watching the video "Routing and MVC", and am now at where Scott H. adds a new folder "Tickets" to "Views" (at the 21:20 minute mark). I have been following his instructions exactly up to this point, and the new index.cshtml file was created under Views/Tickets (which I will explain shortly). However, as this screenshot shows, there is a red squiggly line under ViewData with the associated error

The name 'ViewData' does not exist in the current context

The image shows a total of 8 errors, but I have not made any changes to the file. Neither has Scott in the video up to this point and he is not getting any error with ViewData. What's more, he was able to rename View.cshtml to index.cshtml but it does not work for me (hence I used this name when I created my file, and find that renaming to any other string is discarded without any error except those in the error list).

My situation is similar to the one described in this post in that it is already there by default without needing to do anything, so I suspect it could be attributed to how VS2017 has been set up.

UPDATE!!!

I have continued with the video, seeing no runtime error, and am now nearing the end. By 32:20, Scott H. had added this code to the TicketController class:

    public IActionResult Index()
    {
        // GO TO THE DATABASE!
        // GET SOME MODELS (STUFF)
        var s = new Seat() { Location = "Orchestra" , Price = 300.00 };


        return View();
    }

    public string Index2()
    {
        return "Hello from Tickets!";
    }

Then he modified index.cshtml to reference using @Model as such:

@model WebApplication3.Models.Seat

@{
    ViewData["Title"] = "index";
}

<h2>My New Tickets View</h2>

@Model.Location for only @Model.Price!

When I enter the same code into my editor the squiggly lines appear under @Model with the same error as ViewData:

The name 'Model' does not exist in the current context

Executing the code and navigating to the /tix page does not show Orchestra for only 300! on the webpage as shown in the video, but on my side I get this Internal Server Error instead. As before, I have checked my code and it matches what I see in the video.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Andrew Lau
  • 145
  • 12
  • In your 'Errors' tab, turn off intellisense errors. This will stop the red squiggly line. Intellisense is not as good as the compiler at spotting problems and this one (ViewData not being recognised) is a classic problem. If it causes a 'runtime' error, that's a different problem. – Neil Jan 07 '18 at 21:39

1 Answers1

0

You are missing to pass the model to View. Please pass the 'Seat' model object as shown below.

public IActionResult Index()
{
    // GO TO THE DATABASE!
    // GET SOME MODELS (STUFF)
    var s = new Seat() { Location = "Orchestra" , Price = 300.00 };


    return View(s);
}
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
  • What a fool...didn't notice he added the `(s)` parameter in the video. Had to rewind to look at it again and oh! there it is. Thanks for your answer. – Andrew Lau Jan 08 '18 at 00:19