Tech Mahindra ASP.NET MVC Recent Most common Interview Questions And Answers 2018
Explain the methods used to render the views in MVC?Below are the methods used to render the views from action -
View() — To return the view from action.
PartialView() — To return the partial view from action.
RedirectToAction() — To Redirect to different action which can be in same controller or in different controller.
Redirect() — Similar to “Response.Redirect()” in webforms, used to redirect to specified URL.
RedirectToRoute() — Redirect to action from the specified URL but URL in the route table has been matched.
What are Non Action methods in MVC?
In MVC all public methods have been treated as Actions. So if you are creating a method and if you do not want to use it as an action method then the method has to be decorated with “NonAction” attribute as shown below –
[NonAction]
public void TestMethod()
{
// Method logic
}
How to change the action name in MVC?
“ActionName” attribute can be used for changing the action name. Below is the sample code snippet to demonstrate more –
[ActionName(“TestActionNew”)]
public ActionResult TestAction()
{
return View();
}
So in the above code snippet “TestAction” is the original action name and in “ActionName” attribute, name — “TestActionNew” is given. So the caller of this action method will use the name “TestActionNew” to call this action.
Unlike code expressions that are evaluated and sent to the response, it is the blocks of code that are executed. This is useful for declaring variables which we may be required to be used later.
@{
int x = 123;
string y = “aa”;
}
What is MVC Application Life Cycle?
Any web application has two main execution steps first understanding the request and depending on the type of the request sending out the appropriate response. MVC application life cycle is not different it has two main phases first creating the request object and second sending our response to the browser.
Creating the request object: -The request object creation has four major steps. Below is the detail explanation.
Step 1 Fill route: MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.
Step 2 Fetch route: Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.
Step 3 Request context created: The “RouteData” object is used to create the “RequestContext” object.
Step 4 Controller instance created: This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.
Step 5 Creating Response object: This phase has two steps executing the action and finally sending the response as a result to the view.
What are the advantages of MVC?
A main advantage of MVC is separation of concern. Separation of concern means we divide the application Model, Control and View.
We can easily maintain our application because of separation of concern.
In the same time we can split many developers work at a time. It will not affects one developer work to another developer work.
It supports TTD (test-driven development). We can create an application with unit test. We can write won test case.
Latest version of MVC Support default responsive web site and mobile templates.
List out different return types of a controller action method?
There are total nine return types we can use to return results from controller to view.
ViewResult (View): This return type is used to return a webpage from an action method.
PartialviewResult (Partialview): This return type is used to send a part of a view which will be rendered in another view.
RedirectResult (Redirect): This return type is used to redirect to any other controller and action method depending on the URL.
RedirectToRouteResult (RedirectToAction, RedirectToRoute): This return type is used when we want to redirect to any other action method.
ContentResult (Content): This return type is used to return HTTP content type like text/plain as the result of the action.
jsonResult (json): This return type is used when we want to return a JSON message.
javascriptResult (javascript): This return type is used to return JavaScript code that will run in browser.
FileResult (File): This return type is used to send binary output in response.
EmptyResult: This return type is used to return nothing (void) in the result.
What is MVC HTML- Helpers and its Methods?
Answer:
Helper methods are used to render HTML in the view. Helper methods generates HTML output that is part of the view. They provide an advantage over using the HTML elements since they can be reused across the views and also requires less coding. There are several builtin helper methods that are used to generate the HTML for some commonly used HTML elements, like form, checkbox, dropdownlist etc. Also we can create our own helper methods to generate custom HTML. First we will see how to use the builtin helper methods and then we will see how to create custom helper methods.
Standard HtmlHelper methods
Some of the standard helper methods are:
ActionLink: Renders an anchor.
BeginForm: Renders HTML form tag
CheckBox: Renders check box.
DropDownList: Renders drop-down list.
Hidden: Renders hidden field
ListBox: Renders list box.
Password: Renders TextBox for password input
RadioButton: Renders radio button.
TextArea: Renders text area.
TextBox: Renders text box.
Explain Model in MVC?
Answer:
The model represents the data, and does nothing else. The model does NOT depend on the controller or the view. The MVC Model contains all application logic (business logic, validation logic, and data access logic), except pure view and controller logic. With MVC, models both hold and manipulate application data.
The Models Folder:
The Models Folder contains the classes that represent the application model.
Visual Web Developer automatically creates an AccountModels.cs file that contains the models for application security.
What is GET and POST Actions Types?
Answer:
GET
GET is used to request data from a specified resource. With all the GET request we pass the URL which is compulsory, however it can take the following overloads.
.get(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] ).done/.fail
POST
POST is used to submit data to be processed to a specified resource. With all the POST requests we pass the URL which is compulsory and the data, however it can take the following overloads.
What are strongly typed Helpers?
In the case of normal helper methods we need to provide the string values to the helper methods.As these are string literals so there is no compile time checking and also intellisense support is available.
In contrast strongly typed helper methods takes lambda expressions so they provide
intellisense support and also are type checked at compile time.
Following are the normal and strongly typed helper methods which generates the same HTML
Html.TextBox("Name")
Html.TextBoxFor(model => model.Name).
Post a Comment