Learn MVC Basics

Definition of MVC

The Model View Controller (MVC) pattern is an architectural design principal that separates components of web applications. This separation gives us more control over individual parts of an application. It lets us easily develop modify and test applications.

The ASP.NET MVC Framework is a lightweight highly testable and presentation framework that integrates with existing ASP.NET features, smarter pages and membership based authentication.

The MVC framework is defined in the System.Web.MVC assemblies.

MVC assemblies

MVC framework

In this whenever the user requests a page the first hit goes to the controller.

Then the request goes to the view and a View is returned.

Controller / View

Views
  1. Present data to the user.
  2. Read only view as well as forms.
  3. Minimal display only logic
  4. Most often the views are created from the model data.

Controller

  1. Responds to the request.
  2. Connects the model to the view.
  3. Invokes the model code as appropriate.
  4. Typically controllers read data from a view, control user input and send input data to the model.

Model

  1. Domain Specific representation of data
  2. Business logic
  3. Often the model objects retrieves data from (and stores data to) a database.

How MVC works

MVC works

Flow of M - V - C

Here in the diagram when the user requests a page it either does a GET () or a POST ().

The request goes to the Routing engine.

Then the Controller (Front Controller) is hit.

Then it will see request and return the requested view.

MVC Convention (How to call a Page in MVC)

MVC Convention
Page in MVC
What is Razor?

  1. Razor is markup syntax for adding server based code to a webpage.
  2. Razor is server-side markup syntax much as PHP.
  3. Razor supports C# and VB Programming Languages.
  4. A Razor code block is enclosed in:

    @{ }

    Razor
  5. The code statements end with a semicolon ( ; )
  6. Variables are declared by Var
  7. Strings are enclosed with a Quotation mark.
  8. C# code is case sensitive.
    e.g

    Code

C# file have Extensions with .cshtml

CsharpFile

What id Data Annotation?

In MVC you can validate data in the UI and before sending to the database auto-magically.

If you add Data Annaotation Validators to your model classes then something.

Data annotations are not the only way to specify validation rules, but they're easy to implement.

Add "using System.ComponentModel.DataAnnotations;" to your model.

The following are Data Annotations we can use in a Model:

  1. DisplayName: Provides a general-purpose attribute that lets you specify localizable strings to display.
  2. Required: A value is required
  3. DataType: The data type annotation can be used to specify the data type for validation.
  4. StringLength: Max. Length of array or string data allowed
  5. DisplayFormat: Specify the display format for a property like various formats for a Date property.
  6. ReqularExpression: validate the value of a property by a specified Regular Expression pattern.
  7. Range: Numeric range constraints for the data field value
  8. MaxLength: Specify max length for a string property.
  9. Bind: Specify fields to include or exclude when adding a parameter or form values to model properties.
  10. Compare: The Compares property compares two properties
  11. Key: Denotes one or more properties that uniquely identify an entity.

After this start with the first tutorial.


Similar Articles