简体   繁体   中英

can anyone tell me what is the main difference between @using and @Model in Mvc in view ,what is required when

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.

@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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM