25

To make a view strongly typed we can use @model and @inherit. Can you please tell me what the difference is between both of them?

Edit:

Please see this example.

alex
  • 6,818
  • 9
  • 52
  • 103
user576510
  • 5,777
  • 20
  • 81
  • 144

2 Answers2

32

The difference is as follows: if your view inherits from WebViewPage<T> then your model directive points to T.

In other words, these two are equivalent

@inherits System.Web.Mvc.WebViewPage<ModelClass>

and

@model ModelClass

Reference: http://weblogs.asp.net/scottgu/archive/2010/10/19/asp-net-mvc-3-new-model-directive-support-in-razor.aspx

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • 1
    does it means @@inherits only work with System.Web.Mvc.WebviwPage ? and with @@model we are free to chose class ? – user576510 May 25 '14 at 11:11
  • @user576510: `@model` also means that page inherits from `WebViewPage`. What you specify is the generic parameter. – Wiktor Zychla May 25 '14 at 13:34
  • 3
    The `@model` directive was not working because I have my views in a separate project using a custom virtual provider. The `@inherits` worked like a charm and solved hours or internet digging. Thank you. :-) – Maxime Nov 18 '14 at 19:30
6

They are the same (i.e. indicate strongly-typed model classes) but @inherit is more verbose (because of the full path). @inherit was the only way to do it when razor was first introduced (pre-release of MVC 3 I think), but I haven't seen it used for years.