Asp.Net MVC 5 Interview Questions and Answers | Part-4

Nitish Kaushik
WebGentle
Published in
3 min readDec 14, 2019

--

Here is the 4th part of our Asp.Net MVC 5 interview questions series.

Click here to view Part 1.
Click here to view Part 2.
Click here to view Part 3.

41. What is the Model in Asp.Net MVC?

A model in Asp.Net MVC is a normal class that is used to work with data.
In a dynamic website, if the data is coming from a database or from in-memory (or from other resources) then data traves using models.

42. Can we return multiple models on a single view in Asp.Net MVC?

Using View() method we can return only one model at a time. But this does not mean that we can not use multiple models on a single view.
Yes, we can pass multiple models on a single view.

43 . What are the different ways to pass multiple models on a single view?

There are following popular 6 ways to return multiple models on a single view.

  • ViewModel
  • Dynamic Model (ExpandoObject)
  • Tuples
  • ViewBag
  • ViewData
  • Partial views / @ HTML.RenderAction

Here is great video of passing multiple models on a single view

44. What is ViewModel in Asp.Net MVC?

A ViewModel is a normal class that refers to other models as a property and is mainly used to pass data of multiple models on a single view.

For example, suppose we have these following two classes.

  • Employee.cs
  • Address.cs
public class Employee{public int Id { get; set; }public int Name { get; set; }public int Email { get; set; }}

And

public class Address{public int Id { get; set; }public int EmployeeId { get; set; }public string AddressDetails { get; set; }public string Country { get; set; }}

Now a ViewModel will hold both these classes as property as follows:

public class EmployeeDetails{public Employee BasicInfo { get; set; }public Address Address { get; set; }}

And now simply we can pass EmployeeDetails object in View() method.

var empObj = new EmployeeDetails();return View(empObj);

And on the view we can extract info like this:

@model EmployeeDetails     // Use model directiveModel.BasicInfo            // Get basic profile detailsModel.Address              // Get addtess details

45. What is ViewEngine in Asp.Net MVC?

If you ever notice, When we write C# or VB code on view and once this view gets rendered on the browser then we see only pure HTML.
This all is possible because of ViewEngine.

A ViewEngine in Asp.Net MVC is a special class that is used to perform following tasks.

  • ViewEngine is responsible for creating HTML for views.
  • ViewEngine converts HTML + Programming language (C#, VB) code to pure HTML.
  • ViewEngine is used to find the corresponding view for an action method.
    (Remember we simply write return View() without view name 🙂)
  • ViewEngine is used to find views from Shared folder.
  • Used in HTML helpers, Strongly binding etc.

46. What is Razor?

Razor is the latest and default view engine in Asp.Net MVC 5.

47. When was Razor introduced?

Razor was introduced in Asp.Net MVC 3.

48. What is the namespace for Razor?

using System.Web.Razor;

49. Why Asp.Net MVC application search views only in the “Views” folder?

This is because the Razor view engine is written in such a way that it searches views only in Views folder.

To return view from another folder:

  • Either we can use full path of another folder
  • or, We can create a new View Engine.

50. Can we write C# code on View?

Yes, This is possible because of ViewEngine.

Click here to view Part 1.
Click here to view Part 2.
Click here to view Part 3.

--

--