MVC @model value

in MVC5 what does it mean @model

, @html

and @using

why and when do we usually use it ( @ )

and what word follows it?

For example: @model MVC_Project2.Models.stufftable

written on the first page re.cshtml stufftable is a table that is owned by users to create a new user, and the following code is written on the same page to create two textboxes with two labels with two labels to show the login page:

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(u => u.stuffname)
        @Html.TextBoxFor(u => u.stuffname)
    </div>
    <div>
        @Html.LabelFor(u => u.stuffpass)
        @Html.PasswordFor(u => u.stuffpass)
    </div>
    <input type="submit" />
}

      

+3


source to share


3 answers


in the .cshtml file, everything that goes into it is HTML. Thus, it will be written out exactly the same as written.

In other words, if you just typed

model blah

      

without @

and then when you render the view it will actually render the words model blah

on the page.

The sign @

is a directive telling the Razor engine that the following code is code and it should compile this, not just write it out.

so when you type

@model blah

      

This is razor-compiled and tells the Razor engine that the model type is "blah", so when you use the keyword Model

(note the capital M and you have to use the @ sign as well) it will refer to the model you defined (in this case blah

).

So if you write



@model blah

@Model.Foo

      

then if blah.Foo contains number 14 it will write number 14 to the output. As you might have guessed, the symbol @

has many uses, so if you say @Model.Foo

you are actually doing something like Response.Write(Model.Foo)

.

In general, symbol is @

used to switch from HTML mode to code mode, just like the old ASPX code nuggets used to be <% ... %>

, however the razor is a little smarter and understands the context of your code so that it can conclude where your code ends up most of the time, so no needing to have an ending parenthesis like in the old days.

@using

just like in C # code, it's a using statement that disposes of the available resources after the block ends. Razor uses this technique in many ways to mark the end of a block of code. So, for example, saying:

@using(Html.BeginForm()) {
....
}

      

The helper Html.BeginForm

returns an object that defines an interface IDisposable

that is called when the use statement ends, so BeginForm()

it outputs the tag in that case <form>

, and when the method IDisposable.Dispose()

is called at the end of the using statement, it outputs </form>

. This is the technique I used to wrap other code that outputs tags so that it can properly close its html.

@Html

is also just C #. However, it calls the HtmlHelper object (Razor defines an object called Html in the ViewPage class that supports the view, this Html object is of type HtmlHelper) and it calls various C # extension methods that were defined in the HtmlHelper object. If you don't know what the C # extension method is, this is a way to extend objects without overwriting those objects, this is more advanced C #. Suffice it to say that something seems to be @Html.TextBox()

calling a type method HtmlHelper.TextBox()

, so it's just a C # method that you can call, but those methods are created specifically as helpers to help you create your HTML.

This is actually a lot, and if you don't understand the concepts I was discussing, you really need to learn more about C # and / or HTML as you are probably getting your head around.

+7


source


@

used for directives in Razor code. @model

for example associates a view with a model. @

also used to execute and print C # source code to HTML. When you use it @Html

, it calls a helper class that is part of the Mvc structure that returns MvcHtmlString

.



0


source


Clean and simple: @

- Razor syntax. This will help you insert code C#

into your views (HTML code).
For example:

@DateTime.Now

      

Shows the current date and time.

0


source







All Articles