In this MVC interview questions article, I have collected the most frequently asked questions which are collected after consulting with top industry experts in the field of design patterns, ASP.NET and Spring Framework. If you want to brush up with the MVC basics, which I recommend you to do before going ahead with this MVC Interview Questions, take a look at this article on MVC Architecture.
Top 40 MVC Interview Questions and Answers | Most Frequently Asked ASP.NET MVC Questions | Edureka
12. What are HTML helpers in MVC?
With MVC, HTML helpers are much like traditional ASP.NET Web Form controls.
Just like web form controls in ASP.NET, HTML helpers are used to modify HTML. But HTML helpers are more lightweight. Unlike Web Form controls, an HTML helper does not have an event model and a view state.
In most cases, an HTML helper is just a method that returns a string.
With MVC, you can create your own helpers, or use the built in HTML helpers.
Standard HTML Helpers
The easiest way to render an HTML link in is to use the HTML.ActionLink() helper.With MVC, the Html.ActionLink() does not link to a view. It creates a link to a controller action.
The first parameter is the link text, and the second parameter is the name of the controller action.
The Html.ActionLink() helper above, outputs the following HTML:
The Html.ActionLink() helper has several properties:
HTML Form Elements
There following HTML helpers can be used to render (modify and output) HTML form elements:
ASP.NET Syntax C#
Learn more here – HTML Helpers in MVC: Part 1
29. What is difference between MVC and Web Forms?
Answer – ASP.Net MVC / Web Forms difference
|ASP.Net MVC||ASP.Net Web Forms|
|View and logic are separate, it has separation of concerns theory. MVC 3 onwards has .aspx page as .cshtml.||No separation of concerns; Views are tightly coupled with logic (.aspx.cs /.vb file).|
|Introduced concept of routing for route based URL. Routing is declared in Global.asax for example.||File-based routing .Redirection is based on pages.|
|Support Razor syntax as well as .aspx||Support web forms syntax only.|
|State management handled via Tempdata, ViewBag, and View Data. Since the controller and view are not dependent and also since there is no view state concept in ASP.NET, MVC keeps the pages lightweight.||State management handled via View State. Large viewstate, in other words increase in page size.|
|Partial Views||User Controls|
|HTML Helpers||Server Controls|
|Multiple pages can have the same controller to satisfy their requirements. A controller may have multiple Actions (method name inside the controller class).||Each page has its own code, in other words direct dependency on code. For example Sachin.aspx is dependent on Sachin.aspx.cs (code behind) file.|
|Unit Testing is quite easier than ASP.Net Web forms Since a web form and code are separate files.||Direct dependency, tight coupling raises issues in testing.|
Here are more Similarities and Dissimilarities Between MVC and Web Forms.
40. Server Side Validation in MVC?
The ASP.NET MVC Framework validates any data passed to the controller action that is executing, It populates a ModelState object with any validation failures that it finds and passes that object to the controller. Then the controller actions can query the ModelState to discover whether the request is valid and react accordingly.
I will use two approaches in this article to validate a model data. One is to manually add an error to the ModelState object and another uses the Data Annotation API to validate the model data.
Approach 1 – Manually Add Error to ModelState object
I create a User class under the Models folder. The User class has two properties “Name” and “Email”. The “Name” field has required field validations while the “Email” field has Email validation. So lets see the procedure to implement the validation. Create the User Model as in the following,
After that I create a controller action in User Controller (UserController.cs under Controllers folder). That action method has logic for the required validation for Name and Email validation on the Email field. I add an error message on ModelState with a key and that message will be shown on the view whenever the data is not to be validated in the model.
Thereafter I create a view (Index.cshtml) for the user input under the User folder.
What is ASP.NET MVC interview questions?
- What is MVC (Model View Controller)? …
- What are the advantages of MVC? …
- Explain MVC application life cycle? …
- List out different return types of a controller action method? …
- What are the Filters in MVC? …
- What are Action Filters in MVC? …
- Explain what is routing in MVC? …
- What is Route in MVC?
What is routing in ASP.NET MVC?
What is MVC project in interview explain?