Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Razor difference in @model and @inherit in view header?

Tags:

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.

like image 704
user576510 Avatar asked May 25 '14 10:05

user576510


People also ask

What does @model represent in a razor view?

The @Model will contain all the data of the current page. So when you access a page on your site you can get data from that page with using the @Model.

What does the @model declaration at the top of view do?

The declaration at the top will do two things for you: It will allow intellisence to know what type you are using when you type in @Model or use any of the Html helper extensions. It will also check at runtime that model passed in can be cast to the type the view expects.

What is the difference between razor and MVC?

A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.


1 Answers

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

like image 199
Wiktor Zychla Avatar answered Nov 29 '22 08:11

Wiktor Zychla