Razor Partial Layout Views Recap
Razor Partial Layout Views Recap
Controller
Controller invokes Business Model.
Controller creates object of View Model.
Controller invokes View.
Receives HTTP request data.
Invoke business model to execute business logic.
Creates object of ViewModel and files data into its properties.
Selects a view & invokes it & also passes the object of ViewModel to the view.
Business Model
Receives input data from the controller.
Performs business operations such as retrieving / inserting data from database.
Sends data of the database back to the controller.
View
Receives the object of ViewModel from the controller.
Accesses properties of ViewModel to render data in html code.
After the view renders, the rendered view result will be sent as response.
Views
View is a web page (.cshtml) that is responsible for containing presentation logic that
merges data along with static design code (HTML).
• Controller creates an object of ViewModel and fills data in its properties.
• Controller selects an appropriate view and invokes the same view & supplies
object of ViewModel to the View.
• View access the ViewModel.
• View contains HTML markup with Razor markup (C# code in view to render
dynamic content).
Razor is the view engine that defines syntax to write C# code in the view.
@ is the syntax of Razor syntax.
View is NOT supposed to have lots of C# code. Any code written in the view should
relate to presenting the content (presentation logic).
}
Razor code block is a C# code block that contains one or more lines of C# code
that can contain any statements and local functions.
Razor Expressions
@Expression
--or--
@(Expression)
Razor expression is a C# expression (accessing a field, property or method call)
that returns a value.
Razor - If
@if (condition) {
C# / html code here
}
Razor - if…else
@if (condition) {
C# / html code here
}
else {
C# / html code here
}
Else…if and nested-if also supported.
Razor - Switch
@switch (variable) {
case value1: C# / html code here; break;
case value2: C# / html code here; break;
default: C# / html code here; break;
}
Razor - foreach
@foreach (var variable in collection ) {
C# / html code here
}
Razor - for
@for (initialization; condition; iteration) {
C# / html code here
}
Razor - Literal
<text>static text</text>
Razor - Members
Razor - Methods, Properties, Fields
@functions {
return_type method_name(arguments) {
C# / html code here
}
data_type field_name;
data_type property_name
{
set { … }
get { … }
}
}
The members of razor view can be accessible within the same view.
Html.Raw( )
@{
string variable = "html code";
}
ViewData
It is of Microsoft.AspNet.Mvc.ViewFeatures.ViewDataDictionary type.
namespace Microsoft.AspNetCore.Mvc
{
public abstract class Controller : ControllerBase
{
public ViewDataDictionary ViewData { get; set; }
}
}
ViewBag
ViewBag is a property of Controller and View, that is used to access the ViewData
easily.
ViewBag is 'dynamic' type.
ViewBag is a property of Microsoft.AspNetCore.Mvc.Controller class and
Microsoft.AspNetCore.Mvc.Razor.RazorPageBase class.
namespace Microsoft.AspNetCore.Mvc
{
public abstract class Controller : ControllerBase
{
public dynamic ViewBag { get; set; }
}
}
The 'dynamic' type similar to 'var' keyword. But, it checks the data type and at run
time, rather than at compilation time.
If you try to access a non-existing property in the ViewBag, it returns null.
• You will get Intellisense while accessing model properties in strongly typed views,
since the type of model class was mentioned at @model directive.
• Property names are compile-time checked; and shown as errors in case of
misspelled / non-existing properties in strongly typed views.
• You will have only one model per one view in strongly typed views.
• Easy to identify which model is being accessed in the view.
• Helper methods in Controller to invoke a View
• return View( ); //View name is the same name as the current action method.
• return View(object Model ); //View name is the same name as the current action
method & the view can be a strongly-typed view to receive the supplied model
object.
• return View(string ViewName); //View name is explicitly specified.
• return View(string ViewName, object Model ); //View name is explicitly specified
& the view can be a strongly-typed view to receive the supplied model object.
ViewImports.cshtml
Layout View is a web page (.cshtml) that is responsible for containing presentation
logic template (commonly the html template with header, sidebar, footer etc.)
• The @RenderBody() method presents only in layout view to represent the place
where exactly the content from the view has to be rendered.
• The "Layout" property of the view specifies path of the layout view.
• It can be dynamically set in the view.
• Both View and Layout View shares the same ViewData object.
• So it is possible to send data from view to layout, since the view executes first.
• The css files / js files imported in layout view will be applicable to view also,
because the content of view will be merged into the layout view at run time.
Partial view is a razor markup file (.cshtml) that can't be invoked individually from
the controller; but can be invoked from any view within the same web application.
When partial view is invoked, it receives a copy of the parent view's ViewData
object.
So, any changes made in the ViewData in the partial view, do NOT effect the
ViewData of the parent view.
Optionally, you can supply a custom ViewData object to the partial view, if you don't
want the partial view to access the entire ViewData of the parent view.
Strongly Typed Partial View is a partial view that is bound to a specified model class.
So, it gets all the benefits of a strongly typed view.
PartialViewResult