Can someone tell me what the main difference between @using and @Model in Mvc is, what is required when

I am new to Asp.Net and MVC as such. The only thing that bothers me is that we sometimes use @Using and @Model in our views, I need more clarity about what is required, when and why.

+4
source share
2 answers

@using matches the directive for use in regular C # code: it provides access to namespace types without explicitly specifying.

@model defines the type of model to represent (or partial), allowing printing access to it and its members.

@model refers to the model associated with this view in the current call, as in the actual data.

+6
source

@using used to indicate blocks of code that have objects that implement the IDisposable interface, but it can also be used with HTML helpers in ASP.NET MVC, for example:

 @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, @using display the closing form tag for you.

@model (note the lowercase m ) is used to declare a strong type of model to represent, for example:

 @model YourNamespace.YourTypeName 

Then, in your actual markup, you reference the model using the Model keyword (note the uppercase “M”), for example:

 @Model.SomePropertyInYourModel 
+3
source

Source: https://habr.com/ru/post/1502599/


All Articles