MVC interview Question and Answer Part-1


Question: - What is MVC?
Answer:- MVC is a software architecture pattern for developing web application. It is handled by three objects Model-View-Controller.
Model:-
Applications business logic is contained within the model and is responsible for maintaining data.
View: - It represents the user interface, with which the end user communicates. In short all the user interface logic is contained within the VIEW
Controller:-It is the controller that answers to user actions. Based on the user actions, the respective controller responds within the model and chooses a view to render that display the user interface.  The user input logic is contained with-in the controller

Question: - why MVC?
Answer:-
*.MVC segregates your project into a different segment, and it becomes easy for developers to work on
*.It is easy to edit or change some part of your project that makes project *.less development and maintenance cost
MVC makes your project more systematic

Question: - Is it must to decorate action methods with public access modifier?
Answer: - Yes, every public method will become action methods automatically.


Question: - What about non-public methods?
Answer: - They are simply methods of a class and not available publicly. In simple words these methods cannot be invoked from the web.


Question: - What is the relation between ActionResult and ViewResult?
Answer: - ActionResult is the abstract class whereas ViewResult is the multi-level child of ActionResult. Multilevel because, ViewResult is the child of ViewResultBase and ViewResultBase is the child of ActionResult.

Question: - What is ContentResult?
Answer: - ViewResult represents a complete HTML response whereas ContentResult represents a scalar text response. It’s just like returning pure string. Difference is ContentResult is an ActionResult wrapper around string result. ContentResult is also the child of ActionResult.

Question: - Is it possible to invoke View function without Parameter?
Answer: - Yes, then it will find the view with name “CurrentActionName”.

Question: -what is ViewData?
Answer:-ViewData is a dictionary, which will contain data to be passed between controller and views. Controller will add items to this dictionary and view reads from it.


Question: - Why casting is required?
Answer:-ViewData holds objects internally. Every time a new value is added into it, it gets boxed to object type.
So unboxing is required every time we try to extract value out of it.


Question: - what is ViewBag?

Answer:-ViewBag is just a syntactic sugar for ViewData. ViewBag uses the dynamic feature of C# 4.0 and makes ViewData dynamic.
ViewBag internally uses ViewData


Question:- Disadvantages of ViewData and ViewBag?
Answer:-ViewData and ViewBag is a good option for passing values between Controller and View. But in real time projects it’s not a good practice to use any of them.
Let’s see some disadvantages
No Type safety and no compile time errors
If we try to cast values to wrong type or if we use wrong keys while retrieving the values, we will get runtime error.
No Proper connection between Data sent and Data Received
In MVC, controller and View are loosely connected to each other. Controller is completely unaware about what’s happening in View and View is unaware about what’s happening in Controller.
Performance issues
Values inside the ViewData are of type Object. We have to cast the value to correct type before using it. It adds additional overhead on performance.


Question: - what is ViewModel?
Answer: - ViewModel is one of the unspoken layer in the Asp.Net MVC application. It fits between Model and View and act as data container for View.


Question: - Difference between Model and ViewModel?
Answer: - Model is Business specific data. It will be created based on Business and Database structure. ViewModel is View specific data. It will be created based on the View.




Question: - How Model and ViewModel will be connected?

Answer: - Model and ViewModel should be independent of each other. Controller will create and initialises ViewModel object based on one or more Model object.
MVC interview Question and Answer Part-1 MVC interview Question and Answer Part-1 Reviewed by NEERAJ SRIVASTAVA on 7:13:00 PM Rating: 5

No comments:

Powered by Blogger.