ASP.NET Core MVC Interview

Razor Views, Forms, Tag Helpers & Validation

ASP.NET Core MVC interview questions covering razor views, forms, tag helpers & validation.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
51
Mid-LevelIntermediate

What is Razor syntax in ASP.NET Core MVC?

Razor is the view syntax used by ASP.NET Core MVC to combine HTML markup with server-side C# code in .cshtml files. The @ character is used to transition from HTML into C# expressions, statements, directives, and code blocks.

52
Mid-LevelIntermediate

What is the difference between @model and Model in a Razor view?

The @model directive declares the type of object that a Razor view expects. Model is the runtime property through which the view accesses the actual instance supplied by the controller.

53
Mid-LevelIntermediate

How do Razor code blocks work?

A Razor code block is written with @{ ... } and allows C# statements to execute while the view is being rendered. Local variables can then be used in the surrounding Razor markup.

54
Mid-LevelIntermediate

How do conditional statements work in Razor views?

Razor supports normal C# conditional statements such as if, else if, else, and switch. They allow the rendered HTML to change according to the ViewModel.

55
Mid-LevelIntermediate

How do loops work in Razor views?

Razor supports standard C# loops such as foreach, for, while, and do-while. A foreach loop is commonly used to render a collection from a ViewModel.

56
Mid-LevelIntermediate

What is HTML encoding in Razor, and why is it important?

Razor HTML-encodes normal string output by default. This means characters with special meaning in HTML are encoded before being written to the response, helping protect against cross-site scripting when displaying untrusted text.

57
Mid-LevelIntermediate

What does Html.Raw do, and what security risk does it introduce?

Html.Raw tells Razor to render a string without HTML encoding. It is appropriate only when the content is known to be trusted or has been sanitized by a suitable HTML sanitization process.

58
Mid-LevelIntermediate

What are Razor directives?

Razor directives configure how a Razor file is compiled or behaves. Common directives include @model, @using, @inject, @section, @namespace, @implements, and @addTagHelper.

59
Mid-LevelIntermediate

What is dependency injection in a Razor view?

The @inject directive can inject a registered service directly into a Razor view. This can be useful for presentation-focused services such as localization or simple formatting helpers.

60
Mid-LevelIntermediate

What are Razor sections?

A Razor section defines named content in a child view that a layout can render at a designated location. Sections are commonly used for page-specific JavaScript or styles.

61
Mid-LevelIntermediate

How do you render a section in a Razor layout?

A layout renders a named section using RenderSectionAsync or RenderSection. Setting required to false allows views that do not define the section to render normally.

62
Mid-LevelIntermediate

What is the Form Tag Helper?

The Form Tag Helper generates an HTML form action using MVC routing information. Attributes such as asp-controller, asp-action, asp-route-* and asp-area can describe the target endpoint without hard-coding a URL.

63
Mid-LevelIntermediate

What is the Input Tag Helper?

The Input Tag Helper binds an input element to a model property using asp-for. It uses model metadata to generate attributes such as name, id, type, and validation-related metadata.

64
Mid-LevelIntermediate

What is the Label Tag Helper?

The Label Tag Helper generates a label associated with a model property. With asp-for, it uses model metadata and creates the correct for attribute that corresponds to the generated input id.

65
Mid-LevelIntermediate

What is the Validation Message Tag Helper?

The Validation Message Tag Helper displays validation errors associated with one model property. It uses asp-validation-for to identify the property and reads errors from ModelState.

66
Mid-LevelIntermediate

What is the Validation Summary Tag Helper?

The Validation Summary Tag Helper displays a collection of validation errors from ModelState. The asp-validation-summary value controls which errors are shown, such as model-level errors or all validation errors.

67
Mid-LevelIntermediate

How do data annotation attributes support MVC form validation?

Data annotations placed on an input model provide validation metadata that MVC can evaluate after model binding. Common attributes include Required, StringLength, MinLength, MaxLength, Range, EmailAddress, Compare, and RegularExpression.

68
Mid-LevelIntermediate

What is IValidatableObject in ASP.NET Core MVC?

IValidatableObject allows a model to perform custom validation that can involve multiple properties. Its Validate method returns ValidationResult objects after ordinary property validation.

69
Mid-LevelIntermediate

How do you preserve dropdown values when MVC validation fails?

The POST action must rebuild any supporting data needed to redisplay the form before returning the view. A common example is a list of SelectListItem values for a category dropdown.

70
Mid-LevelIntermediate

How do select elements work with the Select Tag Helper?

The Select Tag Helper uses asp-for for the selected model property and asp-items for the available options. The option list is commonly exposed by the ViewModel as IEnumerable<SelectListItem>.

71
Mid-LevelIntermediate

How are checkboxes bound in ASP.NET Core MVC?

A checkbox can bind to a Boolean property through the Input Tag Helper. MVC's generated markup handles the fact that an unchecked HTML checkbox does not normally submit a value by including the supporting value needed for reliable Boolean binding.

72
Mid-LevelIntermediate

How do you upload files from an MVC form?

An HTML form that uploads files must use multipart/form-data encoding. MVC can bind an uploaded file to IFormFile or multiple files to a collection of IFormFile.

73
Mid-LevelIntermediate

How do you receive an uploaded file in an MVC action?

An action can receive an IFormFile parameter whose name matches the submitted form field. Before storing the file, validate that it exists, enforce an application-defined size limit, validate the allowed content policy, and generate a safe storage name.

74
Mid-LevelIntermediate

What is anti-forgery protection in ASP.NET Core MVC?

Anti-forgery protection helps defend browser-based state-changing requests against Cross-Site Request Forgery (CSRF). The server issues an anti-forgery token associated with the user's request context, and a protected form submission must provide the expected token.

75
Mid-LevelIntermediate

What are best practices for Razor views and MVC forms?

Use strongly typed ViewModels, keep business and database logic out of Razor, and use Tag Helpers for route-aware links and model-aware form controls. Keep views focused on presentation, reuse layouts, partials, and View Components appropriately, and preserve Razor's default HTML encoding.