ASP.NET Core MVC partial view of the (Partial Views)

 

1. What is a partial view

  Partial view is presented in other views view. By performing partial view presented in HTML output generated call view. Like the view, partial view .cshtml use the file extension. When the desired page is reusable parts of a shared between different views, a fragmentary view can be used.

2. When using partial view

  It is a partial view of an effective method for large view into the widget. General layout elements should be specified in _Layout.cshtml, the non-reusable content layout may be packaged as a partial view.

  If a complex page of several logical parts, then each logical part is useful as a fragmentary view. No semantic difference between layout view and normal view, they are just presented in different ways. You can return to the view from ViewResult controller, and this view may also be used as a partial view. The main difference, and detail views are different presentation, partial view does not run _ViewStart.cshtml, and view running.

3. The reference partial view

  There are several ways to render a partial view in the View page. The easiest is to use Html.Partial, and return it to call IHtmlString by @ prefix: @ Html.Partial ( "AuthorPartial").

  The method comprises a partial view of PartialAsync asynchronous code is available: @await Html.PartialAsync ( "AuthorPartial").

  RenderPartial method may also be used to render a partial view. This method does not return a result: it directly to the rendering result output response. Because it does not return the response, it must be called Razor code block. GM also has an asynchronous method RenderPartialAsync:

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

4. Find partial view

  When referring to a partial view, its location can be found by a variety of ways:

// to use the name of the view in the current folder view, if not found, search for Shared Folders 
@ Html.Partial ( " ViewName " ) 

// This view name must be in the same file folder 
@ Html.Partial ( " ViewName .cshtml " ) 

// depending on the application positioned view root path of" / "or" ~ / "represents the beginning of the application path root 
@ Html.Partial ( " ~ / the views / Folder / ViewName.cshtml " ) 

// use of relatively path 
@ Html.Partial ( " ../Account/ViewName.cshtml " )

  Partial view of the possible links. That is, one can call another partial view partial view (they do not create a loop).

The partial view of access data

  When the partial view is instantiated, it gets a copy of the parent view ViewData dictionary. Updates made a partial view of the data will not affect the parent view. When a partial view of return, ViewData partial view of the changes will be lost.

  You can transfer to a local instance of ViewDataDictionary view: @ Html.Partial ( "PartialName", customViewData).

  Model can also be passed to the partial view: @ Html.Partial ( "PartialName", viewModel).

  The model can also be ViewDataDictionary and are passed to the view: @ Html.Partial ( "PartialName", viewModel, customViewData).

6. Simple combat

  Create a model used:

namespace MVCTest.Models
{
    public class Article
    {

      public Article()
      {
        Sections = new List<ArticleSection>();
      }

public string AuthorName { get; set; }
        public List<ArticleSection> Sections { get; set; }
    }

    public class ArticleSection
    {
        public string Title { get; set; }
        public string Content { get; set; }
    }
}

  Then the controller instantiation model:

    public class ArticleController : Controller
    {
        // GET: Article
        public ActionResult Index()
        {
            var article = new Article();
            article.AuthorName = "test";
            article.Sections.Add(new ArticleSection() { Title="title",Content="content"});
            return View(article);
        }

    }

  Parent view:

@model MVCTest.Models.Article

@{
    ViewData["Title"] = "Index";
}

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

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

  AuthorPartial.cshtml:

@model string

<h3>@Model</h3>

  ArticleSection.cshtml:

@model MVCTest.Models.ArticleSection

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

 

Guess you like

Origin www.cnblogs.com/afei-24/p/11293991.html