45

I just created a new MVC Project and created an empty view. I wrote my first line of code while trying to declare the Model for the view which looks like following:

 @model Personal;

Where personal is a Model which actually exists. However, it is continuously showing me following error:

'Personal' is a type, which is not valid in the given context

I have no idea why something so simple can fail. I wish I could post more code but seriously there is nothing more to it, and I'm not sure why it is failing. I have browsed through some of the SO posts, but not one is having the same error at such a trivial location.

Any suggestions?

Lost
  • 12,007
  • 32
  • 121
  • 193
  • What version of Visual Studio are you using? Did you have this view open before creating the model class? – Tieson T. Dec 24 '15 at 04:19
  • I am using Visual Studio 2015 Enterprise. No I did not have any view open before I created the Model – Lost Dec 24 '15 at 04:44
  • You tagged this MVC4 - is that correct? If so, VS2015 doesn't have tooling for MVC4, AFAIK. – Tieson T. Dec 24 '15 at 04:48
  • I am sorry it is MVC 5. I just updated the tag in the question – Lost Dec 24 '15 at 04:51
  • Have you defined the `@model` in any other view like Layout or a partial view? – user1672994 Dec 24 '15 at 06:52
  • 1
    this shouldn't be an issue unless you have some name conflicts - Try using @model <>.Personal. Also, if you can provide information about your model. I.e., namespace and the model declaration. that would be helpful – Nirman Dec 24 '15 at 07:26
  • @Nirman I also tried Model with it still has the same problem. It is not having any issue recognizing the model. It jus throws the error above. – Lost Dec 24 '15 at 17:43
  • The code you have shown would be throwing and _Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement_ error because of the `;` in `@model Personal;` so you have not show the real code. The error your claiming would be caused if you had used `@model Personal()` –  Jan 17 '16 at 08:15

2 Answers2

135
@model <namespace>.Personal

IMPORTANT : Remove the semi-colon from the end of your model declaration.

joby-flick
  • 1,790
  • 1
  • 12
  • 21
0

For me, it was a comment on the same line as the @model statement. Removed the comment and the error went away.

@model <namespace>.Model //A comment here can cause the error.

Edit:

After submitting the above the solution, the error was still there. After trying a few more ideas, I found that the @using statements below the @model statement also caused the error. I removed the @using statements and the error went away. I pasted the @using statements back in the same place and everything compiled and ran fine.

Note: Even with the above edit, a comment on the same line as the @model statement will still cause this error.