Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of detail View of ASP.NET Core MVC

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the partial view of ASP.NET Core MVC example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

1. What is a detail view

A detail view is a view rendered in other views. The HTML output generated by executing the detail view is rendered in the calling view. Like views, detail views use the .cshtml file extension. You can use detail views when you want to share reusable parts of a Web page between different views.

two。 When to use a detail view

Detail views are an effective way to divide large views into small components. General layout elements should be specified in _ Layout.cshtml, and non-layout reusable content can be encapsulated into a partial view.

If a complex page consists of several logical parts, it is useful to use each logical part as a partial view. There is no semantic difference between layout views and normal views, they are just rendered in different ways. You can return the view directly from the controller's ViewResult, and this view can also be used as a detail view. The main difference between a view and a detail view is that it is rendered differently. The detail view does not run _ ViewStart.cshtml, but the view runs.

3. Reference detail view

There are several ways to render a partial view in a view page. The simplest is to use Html.Partial, which is called with the @ prefix and returns IHtmlString: @ Html.Partial ("AuthorPartial").

The PartialAsync method is available for detail views that contain asynchronous code: @ await Html.PartialAsync ("AuthorPartial").

You can also use the RenderPartial method to render the detail view. This method does not return results: it outputs the rendered results directly to the response. Because it does not return a response, it must be called in the Razor code block. Generic also has an asynchronous method, RenderPartialAsync:

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

4. Find detail view

When you reference a detail view, you can find its location in a number of ways:

/ / use the view under the current folder with the view name. If not found, search the Shared folder @ Html.Partial ("ViewName") / / the view with this name must be @ Html.Partial ("ViewName.cshtml") / / locate the view according to the application root path under the same folder. A path that begins with "/" or "~ /" indicates the application root path @ Html.Partial ("~ / Views/Folder/ViewName.cshtml") / / uses the relative path @ Html.Partial (".. / Account/ViewName.cshtml")

Detail views can be linked. That is, one detail view can call another detail view (as long as no loop is created).

5. Detail view access data

When the detail view is instantiated, it gets a copy of the ViewData dictionary of the parent view. Updates to the data in the detail view do not affect the parent view. When the detail view returns, the changed ViewData in the detail view is lost.

You can pass an instance of ViewDataDictionary to the detail view: @ Html.Partial ("PartialName", customViewData).

You can also pass the model to the detail view: @ Html.Partial ("PartialName", viewModel).

You can also pass both the ViewDataDictionary and the model to the view: @ Html.Partial ("PartialName", viewModel,customViewData).

6. Simple actual combat

First create the model you use:

Namespace MVCTest.Models {public class Article {public Article () {Sections = new List ();} public string AuthorName {get; set;} public List Sections {get; set;}} public class ArticleSection {public string Title {get; set;} public string Content {get; set;}}

Then instantiate the model in the controller:

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";} @ Model.AuthorName@Html.Partial ("AuthorPartial", Model.AuthorName); @ foreach (var section in @ Model.Sections) {@ Html.Partial ("ArticleSection", section);}

AuthorPartial.cshtml:

@ model string@Model

ArticleSection.cshtml:

@ model MVCTest.Models.ArticleSection@Model.Title@Model.Content above is all the content of the article "sample Analysis of the partial View of ASP.NET Core MVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report