Imp 4 5
Imp 4 5
MVC stands for Model-View-Controller. It's a design pattern that separates application concerns for
better organization, maintainability, and testability.
• Model: Represents your application's data and business logic. This could be database
interactions, classes holding data, or validation rules.
• View: The user interface (UI) that displays data from the Model. It's responsible for
presentation and should contain minimal logic.
• Controller: Handles incoming requests, interacts with the Model to retrieve or manipulate
data, and selects the appropriate View to render back to the user.
1. User Interaction: A user interacts with the application (e.g., clicks a button, submits a form).
3. Controller Processes: The Controller handles the request. It often interacts with the Model
to retrieve or update data.
4. Controller Selects View: Based on the request and the result of its processing, the Controller
chooses a View.
5. View Renders: The Controller passes data (if necessary) to the View, which then renders the
UI that the user sees.
Control Flow Server-side events, ViewState Explicit control over request handling
Learning
Curve Easier to learn initially Steeper learning curve, but more flexible
5. What is Attribute Routing and how to define it? How do you enable attribute routing in ASP.NET
MVC?
• Attribute Routing: Allows you to define routes directly on controller actions using attributes. This
provides a more localized and readable way to manage routing.
• Enabling in RouteConfig.cs:
6. What is View Engine? What is Razor View Engine?
• View Engine: A component that processes server-side code and data within View files to
generate HTML that's sent to the browser.
• Razor View Engine: The preferred view engine for ASP.NET MVC. It's known for its concise
syntax (using @ for C# code) and efficient code generation.
7. What are HTML Helpers in ASP.NET MVC? What are the different types of HTML Helpers?
HTML Helpers are methods that generate HTML elements within a view. They provide a way to easily
create common HTML elements, such as forms, tables, and lists, without having to write the HTML
code manually.
There are various types of HTML Helpers, some of the most common include:
• Form Helpers: These helpers are used to create forms and their elements, like input fields,
text areas, drop-down lists, and radio buttons.
• Link Helpers: These helpers are used to generate HTML links that point to different actions or
controllers within the application.
• Display Helpers: These helpers are used to display data from a model in a view.
• Validation Helpers: These helpers are used to add validation rules to form elements.
• Image Helpers: These helpers are used to display images within a view.
Layouts are the master templates that define the structure and common elements of your web
pages. They allow you to reuse the same layout for all pages in your application, reducing code
duplication and maintaining consistency.
Layouts typically include the header, footer, and navigation bar, which are shared across all views.
You can define placeholders within a layout where specific content from each individual view will be
inserted.
9. How can we send data from the controller to the view? How can we send data from one action
method to another action method?
There are several ways to send data from the controller to the view:
• Using ViewBag: The ViewBag is a dynamic object that allows you to store data in the
controller and access it in the view.
• Using ViewData: The ViewData is a dictionary object that allows you to store data in the
controller and access it in the view.
• Using Model: The Model is a strongly-typed object that represents the data to be displayed
in the view. It provides compile-time type safety and better code organization.
To send data from one action method to another, you can use the following methods:
• Redirect with parameters: You can redirect to another action method and pass data as query
string parameters.
• TempData: The TempData is a dictionary object that can be used to store data between
consecutive requests. It allows you to temporarily store data in one request and retrieve it in
the next request.
A Non-Action method in ASP.NET MVC is a method within a controller that is not mapped to any URL
route. It's not intended to be invoked directly by a client request. Non-Action methods are used to
perform operations that are not related to handling requests. For example, you might have a Non-
Action method to perform data validation, update data in a database, or call another service.
To mark a method as Non-Action, you can use the [NonAction] attribute in front of the method
definition.
Data Annotations are attributes that you can apply to your model properties to provide metadata
about the data. This metadata can be used for validation, formatting, or other purposes.
• Validation Attributes: These attributes define validation rules for your model properties,
such as Required, Range, and StringLength.
• Display Attributes: These attributes control how your model properties are displayed in the
UI, such as DisplayName and DisplayFormat.