Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC @model meaning

in MVC5 , what does @model, @html and @using mean, why and when we usually use ( @ ) and which word follow it ?

For example : @model MVC_Project2.Models.stufftable is written in the first of re.cshtml page stufftable is a table which is belong to users to create new user and the following code is written in the same page to create two textbox with two labels 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" />
}
like image 248
Shivan.meymo Avatar asked Dec 28 '14 22:12

Shivan.meymo


2 Answers

in a .cshtml file, everything that goes in it is HTML. So it will get written out exactly as its written.

In other words, if you just typed

model blah

without the @ then when you render the view, it will actually display the words model blah on the page.

The @ sign is a directive to tell the Razor engine that what follows is code, and it should compile that rather than simply write it to the output.

so when you type

@model blah

This is compiled by razor, and tells the Razor engine that the type of the model is 'blah', so that when you use the keyword Model (note the capital M and you would have to use the @ sign as well) it will refer to the model you have defined (in this case blah).

So if you write

@model blah

@Model.Foo

then, if blah.Foo contained the number 14, it would write the number 14 to the output. As you might have surmised, the @ symbol has many uses, so if you say @Model.Foo you're actually doing something like Response.Write(Model.Foo).

In general, the @ symbol is used to transition from HTML mode, to code mode, in the same way the old ASPX code nuggets were used <% ... %>, however razor is a little smarter and understands the context of your code so it can infer where your code ends most of the time, so there is no need to have an ending bracket like in the old days.

@using is just like in C# code, it is the using statement that disposes of disposable resources after the block has ended. Razor uses this technique in many cases to signify the end of a block of code. So, for instance saying:

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

The Html.BeginForm helper returns an object that defines an IDisposable interface, that gets called when the using statement ends, so BeginForm() in this case outputs a <form> tag, and when the IDisposable.Dispose() method is called at the end of the using statement, it outputs </form>. It's a technique that's used to wrap other code that outputs tags so that it can properly close their html.

@Html is also just C#. However, it's calling the HtmlHelper object (Razor defines an object called Html in the "ViewPage" class that backs the view, this Html object is of type HtmlHelper) and it calls various C# extension methods which have been defined on the HtmlHelper object. If you don't know what a C# Extension Method is, it's a way to extend objects without those objects having to be rewritten, this is more advanced C#. Suffice it to say, that something like @Html.TextBox() calls a method of type HtmlHelper.TextBox(), so it's just a C# method you can call, but these methods are created specifically as helpers to help you create your HTML.

There is a lot to this really, and if you don't understand the concepts I've discussed, then you really need to learn more about C# and/or HTML, as you are likely getting in over your head.

like image 92
Erik Funkenbusch Avatar answered Nov 10 '22 15:11

Erik Funkenbusch


Clean and Simple: @ is Razor Syntax. It helps you to insert C# code into your views (HTML code).
For Example:

@DateTime.Now

Will show you current date and time.

like image 22
Philip-Otniel Cotan Avatar answered Nov 10 '22 16:11

Philip-Otniel Cotan