MVC Stands for Model-View-Controller. MVC is a software architecture pattern for developing web application. It’s an application design model comprised of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input). Below is a description of each aspect of MVC:
MVVM stands for Model-View-View Model. It supports two-way data binding between view and View model. This enables automatic propagation of changes, within the state of view model to the View. Typically, the view model uses the observer pattern to notify changes in the view model to model.
ViewResult: Renders a specified view to the response stream PartialViewResult: Returns HTML from partial view EmptyResult: empty response or No response JsonResult: Serializes a given View Data object to JSON format JavaScriptResult: Returns a piece of JavaScript code that can be executed on the client ContentResult: Writes content to the response stream without requiring a view RedirectResult: Performs an HTTP redirection to a specified or new URL RedirectToRouteResult: Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data FileContentResult / FileStreamResult / FilePathResult : Represents the content of file
A partial view is a chunk of HTML that can be safely inserted into an existing DOM. Most commonly, partial views are used to componentize Razor views and make them easier to build and update. It can also be returned directly from controller methods. In this case, the browser still receives text/HTML content but not necessarily HTML content that makes up an entire page. As a result, if a URL that returns a partial view is directly invoked from the address bar of a browser, an incomplete page may be displayed. This may be something like a page that misses title, script and style sheets.
Many developers compare routing to URL rewriting since both look similar and can be used to make SEO friendly URLs. But both the approaches are very much different. The main difference between routing and url rewriting is given below:
Routing is a great feature of MVC, it provides a REST based URL that is very easy to remember and improves page ranking in search engines. Creating Route Constraints Suppose we have defined the following route in our application and you want to restrict the incoming request url with numeric id only. Now let’s see how to do it with the help of regular expression. Now for this route, routing engine will consider only those URLs which have only numeric id like as http://example.com/Admin/Product/1 else it will consider that url is not matched with this route.
A View Engine is a MVC subsystem which has its own markup syntax. It is responsible for converting server- side template into HTML markup and rendering it to the browser. Initially, ASP.NET MVC ships with one view engine, web forms (ASPX) and from ASP.NET MVC3 a new view engine, Razor is introduced. With ASP.NET MVC, you can also use other view engines like Spark, NHaml etc. Each view engine has following three main components: For more:
_ViewStart.cshml page is used to serve common layout page(s) for a group of views. The code within this file is executed before the code in any view placed in the same directory. This file is also recursively applied to any view within a subdirectory. ViewStart is executed at the very beginning followed by the start rendering as well as other views. When a set of views shares common settings, the _ViewStart.cshtml file is a great place to put these common view settings. If any view needs to override any of the common settings then that view can set new values to common settings.
RenderBody method exists in the Layout page to render child page/view. It is just like the Content Place Holder on master page. A layout page can have only one RenderBody method. RenderPage method also exists in the Layout page to render other page exists in your application. A layout page can have multiple RenderPage method.
TempData can be defined as a dictionary object used for storing data for a short period of time. Its again a key, value pair as ViewData. This is derived from “TempDataDictionary” class. TempData is used when the data is to be used in two consecutive requests, this could be between the actions or between the controllers. This requires typecasting in view and it has the ability to preserve data for an HTTP request. TempDataDictionary is inherited from the IDictionary<string, object>, ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>> and IEnumerable interfaces. Example
Once “TempData” is read in the current request, it’s not available in the subsequent request. If we want “TempData” to be read and also available in the subsequent request then after reading we need to call “Keep” method as shown in the code below. The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request.
ViewData ViewBag TempData
Razor is the first major update to render HTML in MVC 4. Razor was designed specifically for view engine syntax. Main focus of this would be to simplify and code-focused templating for HTML generation. Razor is not a new programming language itself, but uses C# syntax for embedding code in a page without the ASP.NET delimiters: <%= %>. It is a simple-syntax view engine and was released as part of ASP.NET MVC 3. The Razor file extension is “cshtml” for the C# language. It supports TDD (Test Driven Development) because it does not depend on the System.Web.UI.Page class. sample of using Razor:
What do you know about MVC?
What is a MVVM pattern? And Explain?
Can you tell me the different return types of a controller action method?
What are the pros of MVC?
What are the different MVC components?
Can you explain Partial View in MVC?
Can you explain the different properties of MVC routes?
What is difference between Routing and URL Rewriting?
Can you explain Route Constraints in MVC?
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // Route Pattern
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
} // Default values for parameters
);
}
Restrict to numeric id only
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // Route Pattern
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}, // Default values for parameters
new { id = @"\d+" } //Restriction for id
);
}
Can you explain namespaces in ASP.NET MVC?
Can you explain View Engine and How View Engine works?
Can you explain ViewStart and When to Use it?
@ {
Layout = "~/ Views/ Shared/ _ file.cshtml";
}
<html>
<head>
<meta name="viewport" />
<title> InitialView </title> </head>
<body> ….
</body>
</html>
Can you explain RenderBody and RenderPage in MVC?
<body>
@RenderBody()
@RenderPage("~/Views/Shared/_Header.cshtml")
@RenderPage("~/Views/Shared/_Footer.cshtml")
@RenderSection("scripts",false)
@section scripts{
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
}
</body>
@RenderPage("~/Views/Shared/_Header.cshtml")
Can you explain the methods used to render the views in MVC?
Can you explain TempData in MVC?
public ActionResult FirstRequest()
{
List < string > TempDataTest = new List < string > ();
TempDataTest.Add("Tejas");
TempDataTest.Add("Jignesh");
TempDataTest.Add("Rakesh");
TempData["EmpName"] = TempDataTest;
return View();
}
public ActionResult ConsecutiveRequest()
{
List < string > modelData = TempData["EmpName"] as List < string > ;
TempData.Keep();
return View(modelData);
}
Can you explain the use of Keep and Peek in TempData?
@TempData["interviewgigdata"];
TempData.Keep("interviewgigdata");
string str = TempData.Peek("Td").ToString();
What is the difference between Temp data, View, and View Bag?
What are the different types of Filters in MVC?
Can you explain Razor View Engine?
@model MvcMusicStore.Models.Customer
@{ViewBag.Title = “Get Customers”;}
<div class=”cust”> <h3><em>@Model.CustomerName</em> </h3>
What are the possible file extensions used for razor views?
What are the rules of Razor syntax?
MVC Interview Questions and Answers
For more:
What do you know about MVC?
What is a MVVM pattern? And Explain?
Can you tell me the different return types of a controller action method?
What are the pros of MVC?
What are the different MVC components?
Can you explain Partial View in MVC?
Can you explain the different properties of MVC routes?
What is difference between Routing and URL Rewriting?
Can you explain Route Constraints in MVC?
Can you explain namespaces in ASP.NET MVC?
Can you explain View Engine and How View Engine works?
Can you explain ViewStart and When to Use it?
Can you explain RenderBody and RenderPage in MVC?
Can you explain the methods used to render the views in MVC?
Can you explain TempData in MVC?
Can you explain the use of Keep and Peek in TempData?
What is the difference between Temp data, View, and View Bag?
What are the different types of Filters in MVC?
Can you explain Razor View Engine?
What are the possible file extensions used for razor views?
What are the rules of Razor syntax?