What is ViewData,
ViewBag and TempData?
MVC provides
us ViewData, ViewBag and TempData for passing data
from controller, view and in next requests as
well. ViewData and ViewBag are similar to some extent
but TempData performs additional roles.
What are the roles and
similarities between ViewData and ViewBag?
·
Maintains data when
move from controller to view.
·
Passes data from
controller to respective view.
·
Their value becomes
null when any redirection occurs, because their role is to provide a way to
communicate between controllers and views. It’s a communication mechanism
within the server call.
What are the
differences between ViewData and ViewBag?(taken from a blog)
·
ViewData is a
dictionary of objects that is derived from ViewDataDictionary class
and accessible using strings as keys.
·
ViewBag is a
dynamic property that takes advantage of the new dynamic features in C# 4.0.
·
ViewData requires
typecasting for complex data type and check for null values to avoid error.
·
ViewBag doesn’t
require typecasting for complex data type.
What is TempData?
TempData is a
dictionary derived from the TempDataDictionary class and
stored in short lives session. It is a string key and object value.
It keep the
information for the time of an HTTP Request. This means only from one page to
another. It helps to maintain data when we move from one controller to another
controller or from one action to other action. In other words, when we
redirect Tempdata helps to maintain data between those redirects. It
internally uses session variables. Temp data use during the current and
subsequent request only means it is use when we are sure that next request will
be redirecting to next view. It requires typecasting for complex data type and
check for null values to avoid error. Generally it is used to store only one
time messages like error messages, validation messages.
How can you define a
dynamic property with the help of viewbag in ASP.NET MVC?
Assign a key name with
syntax,
ViewBag.[Key]=[ Value] and value using equal to operator.
For example, you need
to assign list of students to the dynamic Students property
of ViewBag.
List<string>
students = new List<string>();
countries.Add("Akhil");
countries.Add("Ekta");
ViewBag.Students =
students;
//Students is a
dynamic property associated with ViewBag.
What are Scaffold
template?
These templates use
the Visual Studio T4 templating system to generate a view based on the model
type selected.Scaffolding in ASP.NET MVC can generate the boilerplate code we need for
create, read, update,and delete (CRUD) functionality in an application. The
scaffolding templates can examine the type definition for, and then generate a
controller and the controller’s associated views. The scaffolding knows how to
name controllers, how to name views, what code needs to go in each component,
and where to place all these pieces in the project for the application to work.
What are the types of
Scaffolding Templates?
Various types are as
follows,
SCAFFOLD
DESCRIPTION
Empty
Creates
empty view. Only the model type is specified using the model syntax.
Create
Creates
a view with a form for creating new instances of the model.
Generates a label and input field for each property of the model type.
Delete
Creates a view with a form for
deleting existing instances of the model.
Displays a label and the current value for each property of the model.
Details
Creates a view that displays a
label and the value for each property of the
model type.
Edit
Creates
a view with a form for editing existing instances of the model.
Generates a label and input fi eld for each property of the model type.
List
Creates a view
with a table of model instances. Generates a column
for each property of the model type. Make sure to pass an IEnumerable<YourModelType>
to this view from your action method.
The view also contains links to actions for performing the create/edit/delete
operations.
Show an example of
difference in syntax in Razor and WebForm View?
Razor
<span>@model.Message</span>
Web Forms
<span><%: model.Message %></span>
Code expressions in
Razor are always HTML encoded. This Web Forms syntax also automatically HTML
encodes the value.
What are Code Blocks
in Views?
Unlike code
expressions, which are evaluated and outputted to the response, blocks of code
are simply sections of code that are executed. They are useful for declaring
variables that we may need to use later.
Razor
@{
int x = 123;
string y = ˝because.˝;
}
Web Forms
<%
int x = 123;
string y =
"because.";
%>
What is
HelperPage.IsAjax Property?
HelperPage.IsAjax
gets a value that indicates whether Ajax is being used during the request of
the Web page.
Namespace: System.Web.WebPages
Assembly: System.Web.WebPages.dll
However, same can be
achieved by checking requests header directly:
Request["X-Requested-With"]
== “XmlHttpRequest”.
Explain combining text and markup in Views with the help of an example?
This example shows what intermixing text and markup looks like using Razor as compared to Web Forms:
Razor
@foreach (var item in items) {
<span>Item @item.Name.</span>
}
Web Forms
<% foreach (var item in items) { %>
<span>Item <%: item.Name %>.</span>
<% } %>

No comments:
Post a Comment