Asp Net Mvc Experience Interview Questions

1 What are the Main Razor Syntax Rules?


  • Razor code blocks are enclosed in @{ … }
  • Inline expressions (variables and functions) start with @
  • Code statements end with semicolon
  • Variables are declared with the var keyword
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml
  • C# Example

    Learn more here – Introduction to Microsoft ASP.NET MVC 3 Razor View Engine

    2 What is Validation Summary in MVC?


    The ValidationSummary helper method generates an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.

    The ValidationSummary can be used to display all the error messages for all the fields. It can also be used to display custom error messages. The following figure shows how ValidationSummary displays the error messages.

    ValidationSummary() Signature

    MvcHtmlStringValidateMessage(bool excludePropertyErrors, string message, object htmlAttributes)

    Display field level error messages using ValidationSummary

    By default, ValidationSummary filters out field level error messages. If you want to display field level error messages as a summary then specify excludePropertyErrors = false.

    Example – ValidationSummary to display field errors

    @Html.ValidationSummary(false, “”, new { @class = “text-danger” })

    So now, the following Edit view will display error messages as a summary at the top. Please make sure that you dont have a ValidationMessageFor method for each of the fields.

    Learn more here – Understanding Validation In MVC – Part 4

    Explain 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 an 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. The following is a detailed explanation of the same.

    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 rout table with routes collection. This filling of the route table happens 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.

    Creating a Response object

    This phase has two steps executing the action and finally sending the response as a result to the view.

    Here is a complete article on ASP.Net MVC Life Cycle.

    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.

    Top MVC Interview Questions And Answers | ASP.NET MVC Interview Questions And Answers | Simplilearn

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *