In this post, you will understand the most interview questions asked in ASP.NET MVC Interview
What is MVC ?
MVC is an architectural pattern, it’s divided into three broader sections, Model, View, and Controller.
View: The view is responsible for the look and feel.
Model: It provides data to the view.
Controller: It takes the user request, and load the required view and model
What are the Benefits of MVC ?
- Main advantage of MVC is Separation Concern, We divide the application into Model, View and Controller
- Easy to maintain the application
- Automatic UI testing is possible
Is MVC different from a 3-layered architecture ?
MVC is an evolution of 3 layered architecture, Many components of 3 layered architecture are part of MVC.
What is the latest version of Asp.net MVC?
The latest version of ASP.NET MVC is ASP.NET MVC 5.2
What is routing in MVC?
Routing helps you to create a user-friendly URL. which are mapped to the controller of actions and you need to configure the routing in the RouteConfig.cs file. The default route is present in the file but as per need, we can create the custom routes.
routes.MapRoute (name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Where is route mapping code is written ?
Mapping code is written in RouteConfig.cs file
What are the type of routing in MVC?
There are two types of routing in MVC – Conventional or Traditional routing and Attribute-based routing.
Explain the attribute-based routing?
Attribute routing added in MVC 5, In attribute routing, we add the Route attribute on top of the action and add the URL.
[Route("Branch/GetBranch")]
public ActionResult GetBranch()
{
return View();
}
Advantages of attribute-based routing in MVC ?
More user-friendly, flexible, and easy to configure.
How can we maintain the session in MVC?
In MVC we can maintain the session in three ways, 1. TempData 2. ViewData 3. ViewBag
Explain about the Tempdata, Viewdata and Viewbag ?
Tempdata– Pass data between controller to view and view to controller. It maintains the data until it is read.
Viewdata– It transfer data from the controller to view
Viewbag – Pass the data from the controller to view. In viewbag typecasting is not required. Internally it uses the dynamic keyword.
Use of Peek and Keep in TempData?
Once tempdata is read the current request is not available for the subsequent request. If we want to read tempdata and also be available for the subsequent request, then we need to call keep
method
@TempData["Data"]
TempData.Keep("Data");
We can do the same using Peek
method, but the difference is Peek function helps to read
as well as be available for the subsequent request.
string str=TempData.Peek("Data").ToString();
What is Partial View in MVC?
The partial view is a Reusable
view, Which is used inside the other view
. For example, Every website needs Menu, footer, side menu, These views we can create as a Partial view and use in required places.
How to create a partial view and Consume the same?
If you’re using MVC 5 and the latest Visual Studio, then right-click on the shared folder and click on Add then MVC 5 Partial Page(Razor) as shown in below
How to call partial view: We can call the partial view @Html.Partial("_LatestPost")
(_LatestPost
is Partial view name)
How to add Validations in MVC?
One of the ways of doing validation in MVC is Data Annotations. Data annotation is nothing but adding attributes over the model properties.
For example, In the below code snippet added the Email property, We want to apply the required field and email, address validation on the Email field, that’s why we have added the <strong>[Required]</strong>
and <strong>[EmailAddress]</strong>
attribute on Email property.
[Required]
[EmailAddress]
public string Email { get; set; }
Data annotation comes from <strong>System.ComponentModel.DataAnnotations</strong>
namespace.
How to display all the errors in one place?
We can use ValidationSummary
from HTML helper class
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
List of data annotation attributes for validation in MVC ?
There are multiple attributes are present,
DataType – Specify the datatype of property.
Display – Specify the Name of the property.
DisplayFormat – Specify the format of the property for example Date.
Required – Specify the property as required.
ReqularExpression – Validate the property based on the pattern.
Range – Specify the range of property Example – Age
StringLength – Specify the min and max length of the property.
MaxLength – Specify the max length of the property.
Compare – Compare one property with another property Example – Password and Confirm Password.
What is Razor in MVC ?
It’s a lightweight view engine. Till MVC we had only view type i.e. ASPX. Razor was introduced in MVC 3.
Which view engine is better ASPX or Razor?
Razor is preferred because of its lightweight and has easy syntaxes.
What is the Difference between Layout and Master pages ?
Master pages are in ASP.NET webforms, responsible for the look and feel of the webform and Layout is in ASP.NET MVC for Razor view engine.
Explain the Concept of Scaffolding?
Scaffolding is a technique in which the MVC template helps to generate CRUD(Create, Read, Update and Delete) operation code.
How to implement Ajax in MVC?
We can implement it in two ways,
- Ajax Libraries
- jquery(a cross-platform Javascript)