Reputation:

Error using Model reference in strong-typed View in an MVC project

I am trying to create a new strong-typed view for an MVC project.

I tried to create it both from the Controller class (right click -> add View) or directly from the Views (right click -> Add view) and selected it to be a strong-typed view. From the drop-down, I selected the Model and data class it should refer to.

While filling in the content of my View, I need to declare it as:

<p>
   Title:
   <%= Html.Encode(Model.Title) %>
</p>

For some reason, I just get this error: "The name 'Model' does not exist in the current context".

I must be missing something out... :-(
Any comment or idea would be great!

Upvotes: 0

Views: 823

Answers (3)

Pat James
Pat James

Reputation: 4348

I had a similar problem when adding MVC Views to an existing WebForms application. I resolved it by adding a Web.config file to the Views folder, copying the contents from another MVC project.

Upvotes: 1

sean
sean

Reputation: 11624

It should be <%= Html.Encode(ViewData.Model.Title) %>

Upvotes: 1

Joel
Joel

Reputation: 19358

Does your code behind class inherit from ViewPage<ModelType>?

Upvotes: 1

Related Questions