R – ASP.NET MVC Newbie: why isn’t the model available when creating a strongly-typed view

asp.net-mvcstrongly-typed-viewviews

I'm starting with ASP.NET MVC, and working with NerdDinner as reference. I did the following:

  1. Created a new project
  2. Added a couple of tables
  3. Created LINQ to SQL for them
  4. Created a new controller

My Models directory now contains MyModel.dbml, under which I have MyModel.designer.cs, that contains classes for models relating to both of my tables (let's call them Categories and Products).

Now, under my new controller, I would like to create a strongly typed view. So for example I have the following code in my controller (for my application I must work by name, and the "Name" field is unique):

    public ActionResult Details(string name)
    {
        MyModelDataContext db = new MyModelDataContext();
        Product user = db.Products.Single(t => t.Name == name);
        return View(user);
    }

I would like to create a strongly-typed view. So I right-click the line "return View(user)", and choose "Add View…". I click "Create a strongly-typed view". When I click the dropdown of "View data class", I don't see my models, but only:

  1. MyProject.Controllers.AccountMembershipService
  2. MyProject.Controllers.FormsAuthenticationService

What am I missing?

Best Answer

Recompile your project, the just added model classes do not appear in the list until you've recompiled.