Introduction of ASP.NET MVC and AngularJS

33
Introduction of ASP.NET MVC and AngularJS Core concepts and case study Beginner Level By: Mohamed Elkhodary

Transcript of Introduction of ASP.NET MVC and AngularJS

Page 1: Introduction of ASP.NET MVC and AngularJS

Introduction of ASP.NET MVC and AngularJS

Core concepts and case studyBeginner Level

By: Mohamed Elkhodary

Page 2: Introduction of ASP.NET MVC and AngularJS

AGENDA• ASP.NET MVC

• What is MVC?• Separation of concern.• Create a new app.• Add a controller.• Add a view.• Add a model.

• AngularJS• What is AngularJS?• Directives.• Downloading the libraries.• Definitions.

• Case Study• Using ASP.NET MVC and AngularJS in one of company’s product.

Page 3: Introduction of ASP.NET MVC and AngularJS

ASP.NET MVCIntroduction

Page 4: Introduction of ASP.NET MVC and AngularJS

MVC | What is MVC?The ASP.NET MVC is a web application framework developed by Microsoft, which implements the model–view–controller (MVC) pattern.• A model represents the classes that represent the data of the app and that

use validation logic to enforce business rules for that data. .• A controller represents the classes that handle browser requests, retrieve

model data, and then specify view templates that return a response to the view.• A view accepts necessary information from the controller and renders a user

interface to display that information.

Page 5: Introduction of ASP.NET MVC and AngularJS

MVC | Separation of concern• Separate the different aspects of the app (input logic, business logic,

and UI logic).• Provide a loose coupling between these elements. • This separation helps you manage complexity, because it enables you

to work on one aspect of the implementation at a time without impacting the code of another. • For example, you can work on the view code without depending on

the business logic code.

Page 6: Introduction of ASP.NET MVC and AngularJS

MVC | Create a new app

Page 7: Introduction of ASP.NET MVC and AngularJS

MVC | Create a new app

Page 8: Introduction of ASP.NET MVC and AngularJS

MVC | Create a new app

Page 9: Introduction of ASP.NET MVC and AngularJS

MVC | Create a new app

Page 10: Introduction of ASP.NET MVC and AngularJS

MVC | Model – View - Controller

Page 11: Introduction of ASP.NET MVC and AngularJS

MVC | Add a controller

Page 12: Introduction of ASP.NET MVC and AngularJS

MVC | Add a controller

Page 13: Introduction of ASP.NET MVC and AngularJS

MVC | Add a controllerWhen you run the app and don’t supply any URL segments, it defaults to the “Home” controller and the “Index” method specified in the template line highlighted below that can be find in the Startup.cs file.

Page 14: Introduction of ASP.NET MVC and AngularJS

MVC | Add a controller

The controller now doing the “VC” portion of MVC - that is, the view and controller work. The controller is returning HTML directly.

Page 15: Introduction of ASP.NET MVC and AngularJS

MVC | Add a view

Page 16: Introduction of ASP.NET MVC and AngularJS

MVC | Add a view• The Index method below uses a view template to generate an HTML

response to the browser. • Controller methods (also known as action methods), such as the Index

method below, generally return an IActionResult (or a class derived from ActionResult), not primitive types like string.

Controller

Page 17: Introduction of ASP.NET MVC and AngularJS

MVC | Add a view

View

Controller

Page 18: Introduction of ASP.NET MVC and AngularJS

MVC | Add a view

Displayed data “Hello from our View Template!” is hard-coded. The MVC application has a “V” (view) and you’ve got a “C” (controller),

but how to pass dynamic data?

Page 19: Introduction of ASP.NET MVC and AngularJS

MVC | Passing data from the controller to the view

Controller

View

Page 20: Introduction of ASP.NET MVC and AngularJS

MVC | Passing data from the controller to the view

Page 21: Introduction of ASP.NET MVC and AngularJS

MVC | Controller vs View• Controllers are responsible for providing whatever data or objects are

required in order for a view template to render a response to the browser. • A best practice: A view template should never perform business logic

or interact with a database directly. • A view template should work only with the data that’s provided to it

by the controller. • Maintaining this “separation of concerns” helps keep your code clean,

testable and more maintainable.

Page 22: Introduction of ASP.NET MVC and AngularJS

MVC | Add a model• In Solution Explorer, right click the Models folder > Add > Class.

Page 23: Introduction of ASP.NET MVC and AngularJS

AngularJSIntroduction

Page 24: Introduction of ASP.NET MVC and AngularJS

AngularJS | What is AngularJS?• AngularJS or Angular.js is an open-source web application framework

mainly maintained by Google.• A client-side JavaScript Framework for adding interactivity to HTML.• It aims to simplify both the development and the testing of such

applications by providing a framework for client-side model–view–controller (MVC) architecture.

Page 25: Introduction of ASP.NET MVC and AngularJS

AngularJS | What is AngularJS?

How do we tell our HTML when to trigger our JavaScript?

Page 26: Introduction of ASP.NET MVC and AngularJS

AngularJS | What is AngularJS?

How do we tell our HTML when to trigger our AngularJS?

Page 27: Introduction of ASP.NET MVC and AngularJS

AngularJS | DirectivesA Directive is a marker on a HTML tag that tells Angular to run or reference some JavaScript code.

Page 28: Introduction of ASP.NET MVC and AngularJS

AngularJS | Downloading the libraries

https://angularjs.org/

Page 29: Introduction of ASP.NET MVC and AngularJS

AngularJS | Try it in Plunker!

https://embed.plnkr.co/dbCEOw/

Page 30: Introduction of ASP.NET MVC and AngularJS

AngularJS | Definitions

Directives

HTML annotations that trigger JavaScript behaviors

Modules

Where our application

components live

Controllers

Where we add application

behavior behind the DOM

Expressions

How values get displayed within the

page

Page 31: Introduction of ASP.NET MVC and AngularJS

Case StudyLet’s discover how ASP.NET MVC and AngularJS can work together.

Page 32: Introduction of ASP.NET MVC and AngularJS

References• http://www.asp.net/mvc • https://angularjs.org/

Page 33: Introduction of ASP.NET MVC and AngularJS

Thank You