ASP.NET Core Chinese document Chapter IV MVC (3.7) partial view (partial)

Original: ASP.NET Core Chinese document Chapter IV MVC (3.7) partial view (partial)

Original: Partial Views
Author: Steve Smith
Translation: Zhang Hailong (jiechen) , Liu Yi (AlexLEWIS)
Reviewed by: Hsu Teng Yang (Seay) , where the town of Xi , Wei Meijuan (shown signs)

ASP.NET Core MVC support partial view, would be particularly useful if you need to reuse the same page components across multiple different views.

What is a partial view?

Is a partial view in the other views are rendered view. After performing a partial view generated HTML is rendered results to the caller parent view or view. Like view files, partial view files using .cshtml as the file extension.

Notes
If you have a development background ASP.NET Web Forms, then the partial view is more similar to what you've used before a user control

When to use a partial view?

Is an exploded partial view of an efficient way to view large widget. It can reduce the view of the contents and allow repeated reuse view element. Generic layout elements should be written in _Layout.cshtml in. Non-layout (non-layout) can reuse the package contents into the partial view.

If you have a plurality of logical blocks by a complex page, each logical block is a fragmentary view as is useful. Each section of the page can be viewed as independent of the rest, but the page itself will become much easier, because it contains only the overall structure of the page and call the rendering partial views.

Tips
follow in your view, do not repeat themselves in principle .

Defined partial view

Create a partial view similar to creating other views: You're Views add a folder .cshtml file. No semantic difference between the level of the normal view and a partial view, but they are different from the rendering. You can directly from the controller ViewResult returns a view, and this view may also be used as fragmentary views. The main difference lies in the different types of views on the rendering: partial view does not run _ViewStart.cshtml (normal view _ViewStart.cshtml will run more information, please visit. Layout view ).

Refers to a local view

There are several ways to render a partial view of the view. The easiest way is to use Html.Partialit through @to call and returns the prefix IHtmlContent:

@Html.Partial("AuthorPartial")

PartialAsync method comprising partial view asynchronous code (although generally not recommended to do so in the view) is available.

@await Html.PartialAsync("AuthorPartial")

You can use RenderPartial render a partial view. This method does not return result; it directly to the rendering result output response. Just because it does not return a result, it must be called Razor code block (of course, if necessary, you can also call RenderPartialAsyncmethod):

@{
    Html.RenderPartial("AuthorPartial");
}

Because it will direct output, RenderPartialand RenderPartialAsyncmethods may perform better in some scenes. However, it is recommended that you use the most Partialand PartialAsyncthese two methods.

Notes
If your view of the need to implement the code, it is recommended that you use the view components to replace a partial view.

Found partial view

When a partial view of the reference, you can find its place in a variety of ways:

// 以视图名使用当前文件夹下的视图
// 如果没有找到,则搜索 Shared 文件夹
@Html.Partial("ViewName")

// 这个名称的视图必须在相同文件夹下
@Html.Partial("ViewName.cshtml")

// 依据应用根路径定位视图
// 以 "/" 或 "~/" 开头的路径代表应用根路径
@Html.Partial("~/Views/Folder/ViewName.cshtml")
@Html.Partial("/Views/Folder/ViewName.cshtml")

// 使用相对路径定位视图
@Html.Partial("../Account/LoginPartial.cshtml")

If desired, you can store different partial view of the same name in different folders view. When the reference view to the view name (without file extension), a partial view of the view will use it in a folder. Of course, you can also specify the default and is located in Shared partial view of the folder, this view can be any view (provided that the absence of this partial view in their folder) to use. In other words, you can Shared placed under partial view of the default folder, the default partial view of the same name is a partial view of the folder where the file under the current execution view covered.

Partial view of the chain can be continuously used. This means that you can call a partial view of another partial view (as long as you do not create a loop). In each view or a partial view, with respect to the relative path is always located view, rather than root or parent view.

Annotations
If you define a partial view Razor section , parent will not be visible; will be defined in the partial view.

Partial view of access data from

When the partial view is instantiated, its parent view is obtained ViewDataa copy of the dictionary. Changes made in the local copy of the dictionary view dictionary does not affect the parent view. When the returned partial view, in partial view discarded ViewDatacopies.
You can pass ViewDataDictionaryinstance to a partial view:

@Html.Partial("PartialName", customViewData)

You can also transfer model to a partial view. The model can be a page view model (view model), may also be part of view of the model, or by other custom objects. Only need to call Partial/ PartialAsyncor RenderPartial/ RenderPartialAsyncwhen the model simply as the second parameter.

@Html.Partial("PartialName", viewModel)

You can pass an ViewDataDictionaryinstance of the model view and a partial view:

@Html.Partial("PartialName", viewModel, customViewData)

Examples

In the following example, the view of the specified Articletype view model. ArticleThere is a AuthorNameproperty, which is passed to the called AuthorPartial partial view in; as well as a List<ArticleSection>type attribute, which is passed to a dedicated rendering a partial view of this type:

@using PartialViewsSample.ViewModels
@model Article

<h2>@Model.Title</h2>
@Html.Partial("AuthorPartial", Model.AuthorName)
@Model.PublicationDate

@foreach (var section in @Model.Sections)
{
    @Html.Partial("ArticleSection", section)
}

AuthorPartial (in this case / Views / Shared Folder):

@model string
<div>
    <h3>@Model</h3>
    This partial view came from /Views/Shared/AuthorPartial.cshtml.<br/>
</div>

ArticleSection part:

@using PartialViewsSample.ViewModels
@model ArticleSection

<h3>@Model.Title</h3>
<div>
    @Model.Content
</div>

At runtime, these partial views to be rendered to the parent view, the parent view itself shared _Layout.cshtml is rendered, the output results are as follows:

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11915054.html