23 Most Important MVC Interview Questions and Answers

Important MVC interview Question and answer for Freshers and experienced

0
1160

23 Most Important MVC Interview Questions and Answers

1. What is MVC (Model, View, Controller)?

     Model –  is the part of the application, which handles the logics of application data. Often model  retrieve data from database & store data in database.

     View –  that handles the UI part of the application to display data.

     Controller – that handles the user interaction in the application. Mostly controllers read data from view, control user input & send input data to model.

2. What are the Advantages of Using MVC?

     TDD – Test driver development – MVC was desgined to enable testability without requiring dependencies on a web server (IIS), on a database or external classes.

     Stateless – No Viewstate

     Full control over render HTML.

     Seperation of UI & Businesslogic –  So we can concentrate one thing at a time.

     Clear View markup

     Rich UI support (thro’ clientside JS, libraries like JQuery UI)

     Validation is more powerful with dataAnotation & JQuery.

     Avoid antiforgery attacks & XSS vulnerability.

3. Can we share a view across multiple controller.

Yes. Need to put view in the shared folder.

4. How can we plug an ASP.NET MVC into existing ASP.NET application?

To combine ASP.NET MV into existing asp.net application, we need to add some mvc related reference files.

(i)  system.web.routing

(ii) system.web.mvc

(iii) system.web.Abstractions

Then need to create related ASP.NET MVC folders. Then need to config in web.config file.

5. What are the Action Results in MVC (or) Return types of controller action method?

Action method is used to return an instance of any class which is derived from action result class.

  1.  View Result –  used to return a webpage from an action method.
  2.  Partial View Result – used to send a section of a view to be rendered inside another view.
  3. JSON Result – used to return a using which is formatted as JSON.
  4. File Result – used to send binary output as the response.
  5. Empty Result – returns nothing as the result.
  6. Redirect Result – based on URL, it is used to redirect to another controller & action method.
  7. Content Result
  8. Java Script Result – used to return javascript code which will be executed in the user’s browser.

6. What is the use of Json in MVC?

     Javascript object Notation (JSON) is a lightweight object to pass huge data from server to clientside.

Example:

Var JsonResult = new { id = “123”, name = “usefuldesk”};
Return Json (JsonResult, Json Behaviour.Allow Got);

7. How to improve the performance of MVC application?

  • Only use view engine that we require.
  • Removing default HTTP modules in ASP.NET
  • Implementing Cache
  • Implementing HTTP Compression
  • Implementary JQuery UI libraries with google hosted ajax libraries.
  • By combining scripts and other resources
  • Deploying production code in release mode
  • Optimizing URL generations.

8. What is the use of HTML.ActionLink( ), HTML.Action ( ), HTML.RouteLink ( ), HTML.RouteURL( )?

(i) HTML.ActionLink will generate the URL to get to an action using the first matching route by action name.

9. How to initiate CSS in MVC view?

<link href = "@URL.content ["~/styles/style.css")"  rel = “stylesheet” type = “text/css”/>

10. Explain about the Data annotation in MVC.

Datavalidation is one of the main part of our web applications. In MVC applications, we use DataAnnotations for data validation and displaying suitable alert messages to end users.

NameSpace used for DataAnnotation:

System.Component Model.DataAnnotations;

     The Data Annotation namespace includes the following validator attributes.

  • Range
  • Regular Expression
  • String Length
  • Required
  • Validation – The base class of all validation attributes.

Data Annotation in MVC Example:

The below sample shows how to use DataAnnotation (Data validation) in MVC:

Public Class User Model
{
[Display Name (“User Name”)]
[Required (Error Message = “User Name cannot be blank”)]
Public String UserName {get; set;}

[Required [Error Message = “Password cannot be blank”)]
[StringLength (8, Error Message = “Minimum Passwd length is 8 characters”)]
Public string Password {get; set;}

[RegularExpression (@”\w + [-+.’]\w+)*@)”, Error Message = “wrong email format”)]
Public String Email {get; set;}
}

Another Important thing we need to do in MVC viewpage to show validation messages, We need to use validationMessage ( ) method of the HTML Helper class to show validation message in MVC applications.

Example:

@ HTML.TextBox for (model => model.username)
@ HTML.Validation Message (“UserName”)

Note:

We Need to set “Client Validation Enabled” to true in webconfig file inside <appsettings> tag.

<appsettings>
<add key = ‘ClientValidationEnabled” value = “true”>
</appSettings>

11. What is Razor?

Razor is a markup syntax that let you embed server side code (C#) into webpages. Server based code can create dynamic web content on the fly, while a webpage is written to the browser.

12. What are the Razor helper classes in MVC?

(a) Webgrid

(b) Web graphics

(c) Google Analytics

(d) Facebook Integration

(e) Sending Email

(f) Validation

(g) Twitter Integration  etc.

13. How to declare model in MVC view page?

@ model UserRegistrations.Models.User Model

If you return model (for example : usermodel) as list <>, you need to initiate model like below:

@ model List<User Registrations.Models.UserModel>

14. How to Assign a title for a page in MVC view?

@ {
ViewBag.Title1 = “MVC QUESTION And Answers”;
}

15. How to declare the layout view in MVC view page?

@  {
ViewBag.Title = “MVC Interview Questions”;
Layout = “~/views/shared/-Layout.cshtml”;
}

 

Leave a Reply