Professional Documents
Culture Documents
2 PDF
2 PDF
NET Core
Assistant Lecturer
❖ The layouts page in ASP.NET Core helps us to define the common user
interface elements like header, footer & navigation menu etc on the page
at one place and use it everywhere else.
@RenderBody()
Create a New Layout
❖ Right Click on the Shared folder and ❖ This is simple HTML markup, with three
click on Add View. sections Header, Content, and Footer.
❖ This will create the _Layout.cshtml The Content is a place where we want
❖ Index.cshtml ❖ HomeController.cs
Specifying _Layout to Use in the View
❖ AboutUs.cshtml ❖ HomeController.cs
Partial View
❖ Partial view is a reusable view, which can be used as a child view in
multiple other views. It eliminates duplicate coding by reusing same partial
view in multiple places. You can use the partial view in the layout view, as
well as other content views.
Politic
@Html.Partial()
Sport
@Html.Partial()
News News
Technology Weather
@Html.Partial()
News
@Html.Partial() News
Create a New Partial View
❖ @Html.Partial() helper method renders
the specified partial view. It accept
partial view name as a string parameter
_SportPartial.cshtml _PoliticPartial.cshtml
❖ Index.cshtml
2
_TechnologyPartial.cshtml _WeatherPartial.cshtml
3
_ViewStart
❖ Defining the layout in every view is harder to maintain. The simpler way
is to define the layout in the _ViewStart.html file.
❖ There are circumstances where you may not want to use a layout or
may want to use another layout for a specific page. In such cases just
add the Layout back and set it to Null Or set it to Some other Layout.
ViewData and ViewBag in ASP.NET Core
❖ The Views needs to get the data from the controller. One of the ways
in which to pass the data to the view is using the ViewData or
ViewBag object.
ViewData
Controller View
ViewBag
What is ViewData?
❖ The ViewData is a property of the Controller Base class, which returns a
ViewDataDictionary object.
❖ When you invoke the View method in the Controller Action method, the
ViewData is automatically passed to the View.
❖ In the View, you can access the value stored in the Viewdata using the key.
❖ The data stored in the ViewData object exists only during the current
request. As soon as the view is generated and sent to the client, the
Viewdata object is cleared.
How to Use ViewData?
❖ HomeController.cs
❖ Index.cshtml
Output = Hey!
Passing the Object using ViewData
❖ HomeController.cs ❖ Index.cshtml
What is ViewBag?
❖ ViewBag is lightweight method of transferring values to Views from
Controllers.
❖ The ViewBag uses the dynamic feature that was added in C# 4.0. It is
a wrapper around the ViewData and provides the dynamic properties
for the underlying ViewData collection.
❖ HomeController.cs ❖ Index.cshtml
❖ HomeController.cs ❖ Index.cshtml
The difference between ViewData and ViewBag
❖ The ViewData uses the dictionary syntax to access the values, while ViewBag uses the
property syntax.
❖ ViewData requires typecasting for data type other than string, while ViewBag doesn’t
require casting.
Important: The Drawback of ViewData/ViewBag is that they are resolved dynamically at the
runtime. They do not offer any compile-time type checking, Hence more error prone.
Thank You