MVC interview questions


It is collection of MVC interview questions which I have faced in different technical interviews in various companies. You should be aware of differences and similarities among various versions of MVC. It will help you in tracking purpose of having new templates which are introduced in MVC.

Description:In this article, you will find MVC interview questions.
MVC stands for Model-View-Controller. Why MVC? MVC is recommended in most of the cases because it isolates the domain logic from the user interface. Controlled developemnt is possible with the help of MVC.
Questions:
1)What is model-view-controller?
2)Why to use MVC? Are there any specific criterias in which MVC is used?
3)What is the significance of route mapping?
4)How can we maintain session in MVC?
5)What is partial view?
6)What is JSON? How to use in MVC?
7)What is the difference between tempdata,viewdata and viewbag?
8)What is GET?
9)What is POST?
10)What is PUT?
11)What is DELETE?
12)How to perform validations in MVC?
13)What is ActionResult?
14)How to use ajax with the help of MVC?
15)What is Web-API? Why it is used?
16)What is razor?
17)What is master page? Is it layout in MVC?
18)What is task? What are ReadAsync , PostAsync?
19)What are ActionFilters in MVC?
20)What are the controls of HTML5 you have used in MVC project?
21)How you will implement treeview in MVC?
22)How to create hyperlink in MVC?
23)How to implement validations through Model?
24)How to apply data annotation validation in MVC?


Comments

Author: baskar30 Jun 2013 Member Level: Gold   Points : 10

Here i tried to answer few questions which will help the readers.

1)What is model-view-controller?
The Model-View-Controller (MVC) is a pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes.
Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).
View. The view manages the display of information.
Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

4)How can we maintain session in MVC?
var empdetails=vp.GetData();
Session["employee"]=empdetails;

5)What is partial view?
partial view can be compared to user control in Asp.Net Web forms.Which is mainly used for code re-usability.Partial views are reusable in the Header and Footer views. By this ways it helps in reducing code.

6)What is JSON? How to use in MVC?

JSON (JavaScript Object Notation) is a lightweight data-interchange format.It is based on a subset of the JavaScript Programming Language.
JSON is built on two structures
a.A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
b.An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.


7)What is the difference between tempdata,viewdata and viewbag?
TempData

1.TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
2.TempData is used to pass data from current request to subsequent request means incase of redirection.
3.It’s life is very short and lies only till the target view is fully loaded.
4.It’s required typecasting for complex data type and check for null values to avoid error.
5.It is used to store only one time messages like error messages, validation messages
ViewData
1.ViewData is a dictionary object that is derived from ViewDataDictionary class.
2.ViewData is used to pass data from controller to corresponding view.
3.It’s life lies only during the current request.
4.If redirection occurs then it’s value becomes null.
5.It’s required typecasting for complex data type and check for null values to avoid error.
ViewBag
1.ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
2.Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
3.It’s life also lies only during the current request.
4.If redirection occurs then it’s value becomes null.
5.It doesn’t required typecasting for complex data type.
8)What is GET?
Post request is sent via HTTP request body or we can say internally.
GET request is the default method.
Since GET request is sent via URL, so that we can not use this method for sensitive data.
GET request has a limitation on its length. The good practice is never allow more than 255 characters.
GET request will be better for caching and bookmarking.
GET request is SEO friendly.
GET request always submits data as TEXT.

9)What is POST?
We have to specify POST method within form tag like 
Since Post request encapsulated name pair values in HTTP request body, so that we can submit sensitive data through POST method.
POST request has no major limitation.
POST request is not better for caching and bookmarking.
POST request is not SEO friendly.
POST request has no restriction.

10)What is PUT?
Used to update an existing data record

11)What is DELETE?
Used to delete an existing data record

12)How to perform validations in MVC?
public class Employee
{
[Required(ErrorMessage = "Name Required")]
public string Name { get; set; }

[Required(ErrorMessage = "Valid Email Required")]
[Email]
public string Email{ get; set; }
}

13)What is ActionResult?

An action method responds to user input by performing work and returning an action result. An action result represents a command that the framework will perform on behalf of the action method. The ActionResult class is the base class for action results.

Author: baskar30 Jun 2013 Member Level: Gold   Points : 10

15)What is Web-API? Why it is used?
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
To expose your service data to the browsers and as well as all these modern devices apps in fast and simple way, you should have an API which is compatible with browsers and all these devices.

16)What is razor?
Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages.Razor is based on ASP.NET, and designed for creating web applications. It has the power of traditional ASP.NET markup, but it is easier to use, and easier to learn.

19)What are ActionFilters in MVC?
Action filters are defined as attributes and applied to an Action or controller.
a.Authorize filters ensure that the corresponding Action will be called by an authorized user only. If the user is not authorized, he will be redirected to the login page.
b.HandleError will handle the various exceptions thrown by the application and display user friendly message to the user. By default, this filter is registered in Global.asax.

20)What are the controls of HTML5 you have used in MVC project?

Html5Helper A Html5 helper class which contains all the Html5 elements.
Html5MediaHelper A Html5 helper class which contains all the media related elements.
Html5CanvasHelper A Html5 helper class which contains all the canvas related elements.
Html5SvgHelper A Html5 helper class which contains all the svg related elements.

23)How to implement validations through Model?
public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: