@section的ASP.NET MVC解释 [英] ASP.NET MVC explanation of @section

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

问题描述

有关ASP.NET MVC应用程序,可有人请解释为什么,当我将使用 @section 在View?

For an ASP.NET MVC application, can someone please explain why and when I would use @section in a View?

我<一看到href=\"http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx\">this例如,他补充说 @section 到Index.cshtml。是Index.cshtml共享视图?在本例中的code说什么使用 @section code在此查看但不能看?

I saw in this example, he adds @section to the Index.cshtml. Is Index.cshtml a shared View? What in the example's code says "Use @section code in this View but not that View?".

推荐答案

@section 是定义一个内容是从共享视图覆盖。基本上,它是为你调整你的共享视图(Web窗体类似于母版页)的方式。

@section is for defining a content are override from a shared view. Basically, it is a way for you to adjust your shared view (similar to a Master Page in Web Forms).

您可能会发现<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx\">Scott在这个非常有趣的顾的写了。

You might find Scott Gu's write up on this very interesting.

编辑:基于另外的问题澄清

@RenderSection 语法进入共享视图,如:

The @RenderSection syntax goes into the Shared View, such as:

<div id="sidebar">
    @RenderSection("Sidebar", required: false)
</div>

这将被放置在您的视图与 @section 语法:

This would then be placed in your view with @Section syntax:

@section Sidebar{
    <!-- Content Here -->
}

在MVC3 +您可以定义布局文件中直接用于视图或你可以对所有视图的默认视图。

In MVC3+ you can either define the Layout file to be used for the view directly or you can have a default view for all views.

常见的视图设置可以在_ViewStart.cshtml定义类似这样的默认布局视图设置:

Common view settings can be set in _ViewStart.cshtml which defines the default layout view similar to this:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

您还可以设置共享视图在文件中直接使用,如index.cshtml直接在这个片段所示。

You can also set the Shared View to use directly in the file, such as index.cshtml directly as shown in this snippet.

@{
    ViewBag.Title = "Corporate Homepage";
    ViewBag.BodyID = "page-home";
    Layout = "~/Views/Shared/_Layout2.cshtml";
}

有多种方式可以用在<一个提到几个调整此设置href=\"http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file\">this SO回答。

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

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