Table of Contents

Abstract

This article explains what we have presented during the session for ASP.NET in the Delhi Developer's day held on December 28, 2014.

It was an awesome event where all developers were united under the common platform. All enjoyed and tried to explore their knowledge of ASP.NET MVC. I never forget the colder and foggy day, all the developers broke this hurdle and united together to enhance learning. I really appreciate these guys.

History

This session is the second part of the ASP.Net MVC series for beginners. I suggest reading the article ASP.NET MVC Series For Beginners: Part 1 if you that (the first) part of the series.

Here are the topics we covered in this session:

Revisiting previous session

In this section, we revised the things what we have discussed during the previous session, here is the overview of the discussion:

REQUEST RESPONSE

FIGURE 1: REQUEST/RESPONSE

We discussed how a request comes in and a response is sent back in any web application. Here, we discussed some common things like Round Trip, HttpHandler, How requests handled at server side (IIS) and so on.

REQUEST FLOW

FIGURE 2: REQUEST FLOW

We also discussed how requests flow in an ASP.NET MVC application.

PICTORIAL REPRESENTATION OF REQUEST WORKFLOW IN MVC

FIGURE 3: PICTORIAL REPRESENTATION OF REQUEST WORKFLOW IN MVC

Finally, we discussed how request flows in an MVC application.

The preceding is just an abstraction of the previous session, we discussed the things in details. During the session we did something.

Starting with ASP.Net MVC application

In all of our demo apps, we used Visual Studio Preview 2015. In this section we are going to start step-by-step.

Can you think of why we have other folders?

All the other folders have helper files or other supported content.

Understanding MVC flow

Let's get back for a moment and try to understand the MVC app flow. We can also predict from the image that the MVC pattern divides a software application into three interconnected parts/components. Basically, it's a concept of separation (here just the separation of internal representations of information from ways/logics/techniques of how it is being represented to the end-user).

DEFINING MVC

FIGURE 5: DEFINING MVC

As in the preceding image, let's elaborate on Model View Controller (MVC):

A Controller can send commands/directions to the model to update its state as well as commands over to the view.

Associated views and controllers get notified by the Model depending on the changes of its state.

The Model provides specific results as an output upon the request of the View to represent.
DEFINING
FIGURE 6: DEFINING MVC

Let's understand this from the preceding associated image.
In simple words, we can say that MVC is providing a facility to make our layers separate that are separated and interacting with each other.

Models

Returning to our project, we have created the preceding. Have a look at Models.

Add a Model

As we know Model is a class. In Solution Explorer move to the Models folder and add a new class, here is a sample Model:

cs code

Have a look into the preceding class, we have certain properties in the Student class. Now, think of a Student table (that is in your database). This table has a few columns. Now, return to our class defined above. StudentModel is a representation of our Student table and all the properties are the representation of all the table columns.

Attributes at a glance

After creating our data models, in other words after creation of our representation of tables, we need to control all the properties, like what kind of Data Type they could accept, how much length they could accept and so on.

The following are the main attributes available:

Q & A

In this session we have covered a great Q&A section, I am trying to cover each and every question. Please add any question I missed.

I do not have Visual Studio 2015 installed on my System.

I am not going into depth about this, refer to this article for the installation of VS 2015: http://goo.gl/BGFWYw.

Choosing ASP.NET MVC over ASP.NET Web-Forms

There are numerous reasons to choose ASP.Net for web development over ASP.Net Web forms including the following:

Flexibility (provides various view engines that render HTML; the Razor View Engine is the most famous).

Let's think of a scenario where we are writing an application in ASP.NET Web Forms and there are two classes for one thing. Let's say a default page that includes Default.aspx and Default.aspx.cs pages and our designer page.

A designer page has all the server control and aspx pages that contain visualization of our server controls (UI part) and finally our cs page (class) contains all the functionality and other logics. Our class is based on the ASP.NET Page life cycle (for complete details refer to: MSDN).

We can't completely test our Web Forms. To make it properly testable at some instance, we need to implement some other UI Design pattern viz. MVP, MVVM, MVC.

If we are implementing the MVC UI Pattern in our Web Forms then why don't we use the ASP.NET MVC framework that is already using this pattern and provides everything required to create web apps.

Define Model View Controller

Here is the conclusion of what we discussed (for the ASP.Net MVC framework).

Models

Views

Controllers

Routes

A system works on a pattern matching mechanism. A route matches an incoming request to meet a specific pattern and allows once matched else denied.

Conclusion

In this article I tried to cover all the things we discussed about Models and Demo applications. Also, I included some questions/discussions and answers. I am continuously receiving emails with more questions and I will provide more questions/discussions and answers very soon.

Until now, we discussed Models, data attributes and complex models.

We will continue in the next session.