MVC @model meaning

情到浓时终转凉″ 提交于 2019-12-22 09:38:05

问题


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" />
}

回答1:


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.




回答2:


The @ is used for directives in Razor code. @model, for example, binds the View to the model. @ is also used to execute and print out back-end C# code in HTML. When you use @Html it calls a helper class that is part of the Mvc framework which returns an MvcHtmlString.




回答3:


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.



来源:https://stackoverflow.com/questions/27681414/mvc-model-meaning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!