Part 23- RenderBody , RenderSection and RenderPage method in ASP.NET MVC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome back to asp.net MVC tutorial I'm Ashish so this is part 23 of this video series so before proceeding please watch part 21 and part 22 of this video series because in that tutorial we just discuss about the layout in an MVC application and what is the role of scripts. render method and styles. render method okay so now in this tutorial we will discuss about the render body render section and render page in an sp. net MVC application okay so we just we just disc about the layout and here is the structure of layout so in the body tag you will find this render body method and this method is going to render this Pages suppose you have created this homepage about page and contact page then all these pages is going to be rendered by this render body method okay so make sure you have not make the layout equal to null on your page okay so we will discuss about the render section and and let's start with the render body so render body method exists in the layout page to render the child page or view okay and it is just like the contain placeholder in master page okay so if you are a waveform developer then you can consider it as contain placeholder and a layout page can have only one render body method because if you see the syntax the render body does not require any parameter and if you you you use uh two times then definitely it will be ambiguous and it will be confused then which uh which page should be rendered by which method okay and here's the render section so a section allow you to specify a reason of content within layout so if you want to render a particular pce section then you can render it using this render section method so first if Define your section on your page using this atate section your section name and inside that you can place any HTML contain you can also put some references of your script file your CSS file okay and using this method uh at theate render section your section name and pass this uh required colon fils so this parameter is optional but it is very very important render page so render page method also exists in your layout page to render other page exist in your application so you just need to Define your page like uh here my page. CS HTML you just need to Define that and using this render page method you can render it on your layout page so a layout page can have multiple render page but but only one render body so remember this so now we will move to the visual studio and we will see practically so here is your render body method it does not require any parameter and this will uh render my index page and my about page and my contact page so here if you see I have not make it at a null so by default it will use this layout okay so here is the view of my page index page and it is using this share layout and inside the layout I have have used one navigation bar and this is footer you can see here okay so this is my navigation bar and this is my footer okay now come to the render section so I'm going to Define this section on my index page okay so just use section space specify your section name so I'm going to use section name okay and inside the name I will just put my content here just write hello hello section okay and also I can put any uh any reference inside this okay now try to run this so this method is going to give me an exception because I have just defined my section but not used anywhere okay so the following section have been defined but have not been rendered for the layout page so you just need to Define this section on your page sorry you can use this section on your page so using this method just copy this and your section name so this will search this my section inside your index page or or uh whatever page you are going to render okay so I'm just right now I'm just deleting this required parameter okay now let's refresh this so here you go so your section has been rendered using uh render section method okay now just comment it I'm going to do second experiment control Casey and just try to reload it so this time it's going to give me an exception because I have not defined my section okay so section is not defined so this layout has not find any section so that's why if you use this parameter then this will not give you any exception so I have just put whether it is required or not so this time if you if you uh if you whether you define your section or not then and then it will will not give you an exception okay if you defined it then it's okay if you not Define it then then also it's okay okay now continue this and just refresh this so here you can see your exception message so here you go and let's try third experiment on it I'm just control KU I'm just using this section inside my layout so just beneath the so let me check and just put this okay so I have defined this section just beneath this render body method and let's check whether it is working or not okay just refresh refesh your page so this time also it's going to give me an exception of render uh section is not defined so so it is clear that you just need to Define this on your index page only whatever page you are going to use okay so you just Define it your on your page okay so I hope this is clear to you and just use this and you can see uh the visual studio uh this uh by default we have this render section script and you can Define your SCP so uh what is the use of this section and uh what is the benefit of uh this using render section so let's let's see um you have your index page and and uh suppose you have just imported some script file uh you want to load here and suppose you have multiple file on your section okay so if you want to uh use this script when on if and only if you are using this page index page so if you're using this index page then only this file will be loaded otherwise uh on on every Pages you will not uh you you it is not necessary to load this file so to save my uh response time you can use this section okay so if you use this script on your uh layout page then definitely that will be loaded and uh that will also decrease uh increase your load time okay so to increase performance you can use this section okay hope it is clear to you now we will just uh move to the next item that is HTML uh render page just use render page so render page also require uh your suppose you have defined your page any page on your your folder suppose any share folder so I'm using this _ login partial page okay so I will just try to render this page uh this is defined in yeah here so just copy this and pass this okay now uh you just need to specify the full full path inside your render page method okay otherwise it will give you exception so my page is just inside the view folder okay so just pass the view for views slash shared folder slash your uh your page name and do CSH HTML okay so your extension and now let's try to run this now plus continue this and try to run this so you have just created your HTML page and using the render render page method you just rendered your page inside your layout okay so you can use multiple render page inside your layout let's see yeah I have just copied this two to three times now just refresh this so my render uh my render page method is going to render the page three times so in in my page uh register login so it will be uh here for three times so here you go so the render page method has render three three pages because I have just used three render pages okay so u i I can also render this page using this partial view so why we have used this what is the difference between uh this partial html. partial and uh uh this render page okay so the render page directly right to the output stream it does not buffer okay but but it uh it Returns the HTML string here okay so that's why we are using this and this method uh actually uh Sim similar to the HTML do render partial not partial render partial okay so because that also uh directly write to the output stream okay but the syntax is different you you just need to use uh uh in the curly braces you just need to use html. partial render partial okay and you can pass your uh view name here and any parameter so the syntax is different but it can be used uh instead of that we can use this render page if we are supposed to use it on your uh layout page okay so I'm just going to do one experiment here I just cut it here and I'm just going to use this render page on my index page okay so let's see whether it is working or not so this time it may give me an exception let's see hooray it's not uh giving me an exception so it's concluded that you can use your render page method anywhere in your application okay but but you just need to provide your page path your full page path inside this render page method okay so that's all about the render page uh render section and render body okay so I am done here so thanks for watching my tutorial uh if you have any any doubt then you can uh put a comment and uh if you like my video then give a thumbs up and please share my video And subscribe my channel okay thank you very much have a great day
Info
Channel: Technotips Ashish
Views: 40,155
Rating: undefined out of 5
Keywords: RenderBody, RenderPage, RenderSection, Layouts, Mvc, Method, Technotips mvc, Technotips tutorial, ashish mvc, technotips tutorial, rendersection, mvc, layout mvc, asp.net mvc, mvc tutorial, mvc application, contentplaceholder, masterpage, how to, how to create a website, super smash bros for wii u, smash bros
Id: 1S4CS8ZY7Ic
Channel Id: undefined
Length: 15min 15sec (915 seconds)
Published: Sun Dec 25 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.