ASP.NET MVC Interview Questions and Explanations

1) Data annotations are attribute classes under the namespace _______________.

  1. System.DataAnnotations
  2. System.Annotations
  3. System.ComponentModel.DataAnnotations
  4. System.Component

Answer:  c

Explanation: The Data annotations attribute classes are available under the namespace System.ComponentModel.DataAnnotations.

2) The validation rules are implemented in _____________.

  1. Controller
  2. Model
  3. View
  4. None of the Above.

Answer:  b

Explanation: We’ll implement these rules on our model object – and not within our Controller or our View.  The benefit of implementing the rules within model object is that this will ensure that the validation will be enforced via any scenario within our application that uses the model object.  This will help ensure that we keep our code DRY and avoid repeating rules in multiple places.

3) Among the following which script files are required for enabling client side validations?

  1. jquery.validate-vsdoc.js
  2. jquery.validate.min.js
  3. jquery.validate.js
  4. jquery.validate.unobtusive.min.js

Answer:  b and d.

Explanation: For client side validation to work, you will need to ensure that these two <SCRIPT> tag references are in your view:

<scriptsrc=”@Url.Content(“~/Scripts/jquery.validate.min.js”)”type=”text/javascript”></script>
<scriptsrc=”@Url.Content(“~/Scripts/jquery.validate.unobtrusive.min.js”)”type=”text/javascript”></script>

These can be added by writing the following in .cshtml file

@Scripts.Render(“~/bundles/jqueryval”)

4) Client Validations can be turned off by

  1. Removing the Data annotation attributes from the Model Class.
  2. By adding @{  Html.EnableClientValidation(false); } in the view page.
  3. By adding @{ Html.EnableClientValidation = false; } in the view page.
  4. None of the Above.

Answer:  b

Explanation: To turn off client side validation in view we need to add following

@{  Html.EnableClientValidation(false); }

5) Explain the reason why we use Metadata Class?

Answer: To provide metadata for properties that you don’t have control over, you can create a separate metadata class that has those same property names. You can tell the framework to use this class instead of your model object by providing the [Metadata Type] attribute.

Top 5 ASP.NET MVC Interview Questions and Answers

1) Which option will you use to persist data from one controller to the other?
a. ViewData
b. ViewBag
c. TempData
d. None of the Above

Answer: c

Explanation: Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.

2) ViewBag is of Type _________.
a. var
b. dynamic
c. object
d. None of the Above
Answer: b

Explanation:  It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.

3) ______________ is used to retain key in TempData in Subsequent round trips.

a. Tempdata.keep()
b. Tempdata.save()
c. Tempdata.render()
d. Tempdata.hold()

Answer:  a

Explanation: TempData.Keep() is used to retain key in TempData in the subsequent round trips.

4) [ActionName(“Start”)]
public Actionresult Index()
{
Viewbag.Message = “Welcome”;
return view();
}

For the above method what is the url request that renders the view.
a. {Controllername}/Index
b. {Controllername}/Start
c. {Controllername}/Index or {Controllername}/Start”
d. None of the Above

Answer:  b

Explanation :For the Index method we have below we no longer reach this method as action name “Index”. We have to reach this method as “Start“. (http://localhost:123/Home/Start).      A View with name “Start” must be added. Index view will not work.

5) OnActionExecuting is a method which will be executed after and before ___________?

  1. This method is called before the ActionResult instance that is returned by your action is invoked.
  2. This method is called after the ActionResult instance that is returned by your action is invoked.
  3. This method is called before a controller action method is executed.
  4. This method is called after a controller action method is executed.

Answer:  c

Explanation:  An action filter is implemented as an attribute class that inherits from ActionFilterAttribute.

The base ActionFilterAttribute class has the following methods that you can override:

  • OnActionExecuting – This method is called before a controller action method is executed.
  • OnActionExecuted – This method is called after a controller action method is executed.
  • OnResultExecuting – This method is called before the ActionResult instance that is returned by your action is invoked.
  • OnResultExecuted – This method is called after the ActionResult instance that is returned by your action is invoked.

Understanding ASP.NET MVC?

This article is for the beginners who do not have much idea about MVC. As a beginner all you’ll be knowing is that MVC is a pattern but how do we develop applications using this MVC pattern.

Introduction:

So before understanding what MVC is lets understand first what ASP.NET is?

So ASP.NET is a framework which creates web pages and web sites with the help of html, css, and javascript and severs script.

ASP.NET supports 3 development models:

  1. Web Pages
  2. MVC
  3. Web Form

 

MVC (Model View Controller):

MVC is one of the development models of ASP.NET.

  • The Model represents the data of application (database records)
  • The View displays the data
  • The controller handles the flow of data

MVC Model defines application within 3 logic layers.

  • Input logic(Controller handles Input Logic)
  • Business Logic(Model  handles logic of application data)
  • UI Logic(View display the application data)

 

Model:

I.            This part of application manages data of the application or state of data, i.e. basically model object retrieves data from database.

II.            There is no fixed size or shape of model object because data of one application may not be same as other application.

III.            It includes Validation logic, business logic  and data access logic of an application

E.g. Student object (Model) might retrieves information from database, do some operation on it, validate it and then update information back to Student table in database.

 

Controller:

This part of application controls the flow of application data. Basically it reads from a view, control user input and sends data to model.

 

View:

This part of application display the data, basically it creates UI for data from model and display the data

E.g.: Edit View for Student table that displays textboxes, dropdown list and checkbox on the current state of a Student object.

Advantage:

I.            MVC helps in managing complex task by focusing on one aspect at time, a developer can work on model without depending on views or controller.

II.            Separating the view from rest of application logic enables changing of view technology in future without affecting the rest of the application.

E.g. Client might want view in Silverlight or HTML5

 

MVC vs Traditional Web Form:

I.            It is lighter than Web from because it does not use view state or server based forms or server controls.

II.            It provides better support for test-driven development (TDD)

III.            It has integrated with all existing Asp.net features like Routing, Master Page, Security and Authentication.

So I hope this article has given you a brief idea about what exactly ASP.NET MVC is…..