Reputation: 880
I am new to Asp.Net and MVC as such . The one thing that keeps confusing me is that we use @Using and @Model in our views sometimes , i need more clarity on what is required when and why.
Upvotes: 4
Views: 1594
Reputation: 34846
The @using
is used to designate blocks of code that have objects which implement the IDisposable
interface, but it can also be used with HTML helpers in ASP.NET MVC, like this:
@using (Html.BeginForm()) {
// Do stuff in the form here
}
This is equivalent to:
@{ Html.BeginForm(); }
// Do stuff in the form here
@{ Html.EndForm(); }
So, in this case, the @using
will render the closing tag of the form for you.
The @model
(notice the lowercase m
) is used to declare the strong type the model is for the view, like this:
@model YourNamespace.YourTypeName
Then in your actual markup, you reference the model using the Model
keyword (notice the uppercase 'M`), like this:
@Model.SomePropertyInYourModel
Upvotes: 3
Reputation: 1920
@using
is the same as as the the using directive in normal C# code: it gives access to Types of a Namespace without having to explicitly stating it.
@model
defines the type of the Model for the view (or partial), allowing typed access to it and it's members.
@Model
accesses the model linked with this view in the current call, as in the actual Data.
Upvotes: 6