Register Login

MVC Interview Questions and Answers

Updated Jun 20, 2019

What is MVC and what does it stand for?

MVC stands for Model View Controller. It is a very commonly used application architecture that divides the entire application into 3 parts namely, the model, the view and the controller.

It is used to develop user interfaces and helps in the separation of the internal functionalities. The concept is used to develop scalable web applications.

What is MVC architecture?

The MVC architecture is divided into 3 parts:

  • Model – It is the lowest level of the architecture and it maintains the data of the application.
  • View – It is the user interface that is displayed to the user and it lets them modify it. It displays the data using the model.
  • Controller - It is responsible for managing the interaction between the view and the model.

How to pass a model from view to controller using JSON?

To pass model from view to controller, if a complex object has to be sent, the action method has to be converted into an HttpPost. Use the POST method to post the data.

What is scaffolding in MVC?

Scaffolding is a code generation framework use along with ASP.Net for making web applications. It is used to creating, read, update and delete operations. It is added to the include code for interaction with the data models. This auto-generated code can be optimized according to the requirements.

How to create a master page in MVC?

The steps to create a master page in MVC are:

  1. Create an MVC
  2. In the Share folder, add an MVC layout page.
  3. In the Layout page, add the Nav tags, @RenderSection, @RenderBody, Header, and Footer.
  4. Add view and controller to develop your page.
  5. While creating the View, choose a new layout page.
  6. In the page View, include the page content and the Render section.
  7. Using the Route Config, add code to modify the Controller to set it as default.
  8. Run the project.

What is a controller in MVC?

The controller is responsible for managing the interaction between the view and the model. When a request is raised by the user after interaction with the view, the controller handles it. It receives the request from the View, and based on it performs the actions using the Model.

What is the model view controller design pattern?

The model view controller design pattern is used to separate the application into 3 parts:

  • Model – It depicts an object that has some data, it does not have logic to represent the data to the user.
  • View – It is used to represent the data contained in the Model.
  • Controller – It is used to respond to the requests received from the View. It controls the data of the object.

What are some advantages of the model-view-controller (MVC) pattern?

The advantages of the model-view-controller (MVC) pattern are:

  • Web applications can be developed faster as MVC supports parallel development.
  • Large scale web applications can be developed faster.
  • As MVC offers multiple views for a model, the duplication of code is decreased as business logic and data are separated from the display.
  • The architecture supports SEO friendly web pages.

What is view model and how is it different from the controller?

The ViewModel is used to hold the state of the View and the presentation logic. It encapsulates the state and the logic. It acts as a bridge between the Model and the View. This pattern is optional.

The Controller is a primary component of MVC and is used to performs actions in response to the user requests. It controls the flow of the application.

How to update labels in the model view controller?

Labels can be updated in MVC using the HTML.label, HTML.DisplayFor and HTML.LabelFor functions.

What is routing in MVC?

Routing is used in MVC to decide the action method of the controller class that has to be run. It helps to describe the URL that can be mapped with the request handler. It sends an HTTP request to a controller and the process is performed in System.Web.Routing.

What is an action filter in MVC?

An action filer is used to apply an application logic that runs before and after the controller action executes. These attributes can be applied to the entire controller or the just the controller section and can be used with fields, methods, classes, and properties. The 3 action filters of MVC ASP.NET are Authorize, Output Cache and Handle Error.

What is partial view in MVC?

Partial View in MVC is used to display only a part of the view content. It is reusable and can be used in multiple places that help in reducing the code duplication. It works as a child view and helps us see a view within a parent view. The partial view is initialized with a copy of the ViewDataDictionary object.

What is dependency injection in MVC?

The dependency injection in MVC is a design pattern that is used to separate the responsibilities of making dependencies in a class. As a result, there is no dependency between the two implementations. It is a method of inversion of control.

It is very useful as no code has to be altered if an object depends on it, reducing the complexity of the application.

How to pass data from view to controller in MVC?

Data can be passed from View to Controller by using a ViewModel. In the GET Action, put the following code:

public ActionResult Report()

{

return View(new ReportViewModel());

}

Use the code for the View:

@model ReportViewModel

@using(Html.BeginForm())

{

Report Name : @Html.TextBoxFor(s=>s.Name)

<input type="submit" value="Generate report" />

}

In your HttpPost method in the controller, use:

[HttpPost]

public ActionResult Report(ReportViewModel model)

{

//check for model.Name property value

//return value

}

How to use ViewBag in MVC?

ViewBag in MVC is used to transport temporary data from the controller to the View, which is not incorporated in the model. Being a dynamic type property, it utilizes the latest dynamic features of C# 4.0. Its life lasts as long as the HTTP request runs. It is a ViewData wrapper. Multiple values and properties can be allocated to ViewBag.

What is CSRF attack and how can we prevent the same in MVC?

CSRF (Cross Site Request Forgery) is a process by which the hacker sends data from a malicious website to a genuine site, and that site assumes that the data received is from a proper source. So, the request is run and the genuine site gets hacked. It can be prevented using an anti-forgery token.

How to handle exceptions in Spring MVC framework?

Exceptions are handled in Spring using the following ways:

  • Controller-based – Here, the @ExceptionHandler annotation can be used to write exception handling methods in the controller classes.
  • Global Exception Handler – The @ControllerAdvice can be used to write the global exception handlers and can be used with any class.
  • HandlerExceptionResolver – This is an additional way to create a global exception handler.    


×