ASP. NET core MVC Partial View Application

Source: Internet
Author: User

ASP. NET Core MVC supports partial view features, which are useful when we share some pages in different projects.
1. Partial View definition:
Part of the view is to nest another view in one view. Renders the child view's HTML to the parent view by calling the child view in the parent view.
Like a general view, some views use the. cshtml file name extension.
2. When to use partial views:
Partial views are an efficient way to break large views into smaller view components. Enables the view elements to be reused. Usually layout elements in _layout.cshtml
specified in the. Non-layout and repeatable content can be encapsulated into a partial view.
If you have a complex interface with different logic to make this interface, you can abstract different logic, as part of the view, different parts of the interface can be isolated.
and the entire page becomes simple, so that a complex page can invoke different partial view for rendering.
Hint: We should follow a design principle "do not Repeat yourself";
3. Declare part of the view
The creation of partial view is no different from other view creation: You can create a. cshtml file under the View folder. Partial view is no different from regular view
They only have different rendering methods. You can return a viewresult to the view from the controller. The difference between normal view and partial view is that partial view does not run _VIEWSTART.CSTHML.
(_viewstart.cshtml can be seen from the first name, starting with a view, which is to execute the _viewstart.cshtml file before rendering any view)
4. Quote a partial View
In a view page, there are several ways to render a partial view. The simpler approach is to use html.partial, which returns a ihtmlstring that can be prefixed by using @
To quote:
@Html. Partial ("Authorpartial")
Partialasync is an asynchronous method
@await Html.partialasync ("authorpartial")
You can use RenderPartial to render partial views (Renderpartialasync is an asynchronous version). This method directly returns the result, which is output directly as a stream.
@{
Html.renderpartial ("authorpartial");
}
Because RenderPartial and Renderpartialasync are returned directly as streams, it is better in terms of performance, however, in most scenarios we recommend using partial and Partialasync
5. Find a Partialview
When referencing a partial view, there are several ways to locate a partial view.
@Html. Partial ("ViewName") first searches the current directory for ViewName, if not found, then share directory to find ViewName name. If none of the two directories
The error will be.
The current directory is displayed as follows:

@Html. Partial ("viewname.cshtml") view must be in the same directory (called according to the file name).

Call the home file as follows:

The results appear as follows:



We copied a copy of the partialviewtest to the shered directory and changed the partialviewtest under the home to PartialViewTest1.
The test results are as follows:

The results appear as follows:

The following two ways you can define a partial view in a different folder:
@Html. Partial ("~/views/folder/viewname.cshtml") is located according to the application's root directory.
@Html. Partial ("/views/folder/viewname.cshtml")
@Html. Partial (".. /account/loginpartial.cshtml ") uses relative paths to locate.


6. Accessing data in partial views

When a partial view is instantiated, it gets a copy of the parent View ViewData dictionary. You can update part of the view data without affecting the parent view data.

When a partial view returns, the view data in some views changes.

You can pass to a partial view by instantiating a viewdatadictionary:

@Html. Partial ("Partialname", Customviewdata)

You can also pass an entity to a partial view:

@Html. Partial ("Partialname", ViewModel)

You can also pass a viewdatadictionary and entity to a partial view at the same time:

@Html. Partial ("Articlesection", section,
New Viewdatadictionary (this. ViewData) {{"index", Index}})

The following code displays the Views/articles/read.cshtml view, which contains two partial views. Two parameters were passed to the second partial view.

@{    viewdata["Title"] = "Read";} @using Microsoft.AspNetCore.Mvc.ViewFeatures; @using viewcomponentsimple.models; @model article

The results of the operation are as follows:

Original: Https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/partial


ASP. NET core MVC Partial View Application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.