MVC @model意义 [英] MVC @model meaning

查看:266
本文介绍了MVC @model意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC5,什么是 @model @html @using 意思是,为什么当我们平时使用(@)和字跟随它?

例如: @model MVC_Project2.Models.stufftable 写在第一页re.cshtml的
stufftable中的表是属于用户创建新的用户及以下code被写入在同一页面中创建两个文本框有两个标签,两个标签,以显示登录页面:

  @using(Html.BeginForm())
{
    < D​​IV>
        @ Html.LabelFor(U => u.stuffname)
        @ Html.TextBoxFor(U => u.stuffname)
    < / DIV>
    < D​​IV>
        @ Html.LabelFor(U => u.stuffpass)
        @ Html.PasswordFor(U => u.stuffpass)
    < / DIV>
    <输入类型=提交/>
}


解决方案

在.cshtml文件,该进去它是HTML的一切。因此,这将得到完全写出来作为其写入。

在换句话说,如果您刚键入

 模型等等

没有 @ 然后当你渲染视图,它实际上将显示在页面上的文字模型等等

@ 标志是一个指令告诉剃刀引擎接下来是code,它应该编译,而不是简单地将其写入到输出。

所以,当你键入

  @model嗒嗒

这是由剃须刀编制,并告诉剃刀引擎,该模型的类型是胡说,这样当你使用关键字模式(注意是大写M和你将不得不使用@符号以及)将参考您所定义的模型(在这种情况下,等等)。

所以,如果你写

  @model嗒嗒@ Model.Foo

然后,如果blah.Fo​​o包含在14号,这将写出数14到输出。正如你可能已经猜到,在 @ 符号有很多用途,所以如果你说 @ Model.Foo 你实际上做类似的Response.Write(Model.Foo)

在一般情况下, @ 符号用来从HTML模式转型,code模式,以同样的方式老ASPX code掘金使用<%......%> ,但是剃须刀是一个小聪明,理解你的code的范围内,因此可以推断在您code结束的大部分时间,因此没有必要有一个结束托架像在旧

@using 就像在C#code,它是using语句块结束后可支配资源的处置。剃刀使用在许多情况下,这种技术来表示code的块的结束。因此,例如说:

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

Html.BeginForm 助手返回定义的IDisposable 接口的对象,被调用using语句时,结束,所以 BeginForm()在这种情况下输出&LT;形式&GT; 标记和<$ C $时C> IDisposable.Dispose()方法被调用在using语句的结束,它输出&LT; /形式为GT; 。这是一个的用于包装等code,输出标记,以便能够正确地关闭他们的HTML的技术。

@Html 也只有C#。然而,它调用的HtmlHelper对象(剃刀定义在备份视图中的的ViewPage级称为HTML对象,该HTML对象的类型的HtmlHelper)和它调用已在的HtmlHelper对象上定义的各种C#扩展方法。如果你不知道C#的扩展方法是什么,它​​,而不必重新编写这些对象扩展对象的一种方式,这是更高级的C#。我只想说,这有点像 @ Html.TextBox()调用类型的方法 HtmlHelper.TextBox() ,所以它只是一个C#方法,你可以打电话,但是这些方法是专门创建为助手帮助您创建的HTML。

有很多这种真正的,如果你不明白我讨论的概念,那么你真的需要更多地了解C#和/或HTML,因为你很可能在你的头上进入。

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

解决方案

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.

这篇关于MVC @model意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆