During this session of ASP.NET Core Tutorial we discuss about Partial View and the way that we can call a Partial View within or main Views. Also, we will talk about differences between calling methods.
What is Partial View?
Partial View is reusable part of main View with specific logic that can be used in different Views. Thus, due to remove duplicate codes, we can create a Partial View from that part and call it in different Views.
Partial View discovery is same as Views that we described during Session 17. Moreover, you can change this default discovery as we discussed earlier with entering full path of Partial View. Also, structure and file extension of Partial View is same as View (cshtml for C# programming language and vbhtml for VB programming language).
Hence, based on above default discovery, it is recommended to create our Partial View inside Shared folder in case we have plan to call this Partial View in different Views from different Controllers.
Create Partial View
First, we create a Shared folder inside Views folder, then we create a View inside created folder. Lastly, we insert the peace of code that we want to have as Partial View.
Hence, based on above default discovery, it is recommended to create our Partial View inside Shared folder in case we have plan to call this Partial View in different Views from different Controllers.
Call Partial View with HTML Helper
In this session we introduce four HTML Helper that we can use to call our Partial View inside our main View. But two of them (Html.Partial & Html.RenderPartial) are not recommended as they are Synchronize method as may cause Deadlock. Having said that, overloads of Synchronize and Asynchronous version of both html helpers are the same and input parameters are equal.
Html.Partial – Synchronize Method
Html.PartialAsync – Asynchronous Method
Html.RenderPartial – Synchronize Method
Html.RenderPartialAsync – Asynchronous Method
Just bear in mind, in case of using Asynchronous methods, we should user await prefix. Also, Html.Partia and Html.PartialAsync should be started with @ and they don’t need to be terminated with semicolon “;“. But Html.RenderPartial and Html.RenderPartialAsync should be inside C# code block @{} and we should add semicolon “;” to the end of these method code line.
Furthermore, Html.Partia and Html.PartialAsync have IHtmlContent result which return to main View (the view that call partial view), but, Html.RenderPartial and Html.RenderPartialAsync result is void and the result will be shown in final response as an Html.
Differences between Html.PartialAsync (Html.Partial) and Html.RenderPartialAsync (Html.RenderPartial)
Html.PartialAsync (Html.Partial) have IHtmlContent Result but result of Html.RenderPartialAsync (Html.RenderPartial) are void.
Html.PartialAsync (Html.Partial) return the result to main View but Html.RenderPartialAsync (Html.RenderPartial) present the result to final response as an HTML.
In theory, performance of Html.RenderPartialAsync (Html.RenderPartial) are better than Html.PartialAsync (Html.Partial)
Html.PartialAsync (Html.Partial) should have S@ prefix and no need to be ended with semicolon “;“, but Html.RenderPartialAsync (Html.RenderPartial) should be encapsulate with C# code block and terminated with semicolon “;“
Tutorial Playlist
https://bit.ly/3lxQP9n
Our Social Media
Facebook: https://bit.ly/310ksqA
Instagram: https://bit.ly/3cOq4th
Telegram: https://bit.ly/3s4mzWc
Text Version of course:
https://www.goldencourses.net/partial...
In questa pagina del sito puoi guardare il video online Partial View – Session 23 della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Golden Courses 16 luglio 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 119 volte e gli è piaciuto 2 spettatori. Buona visione!