BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Introduction to MVC Action Methods, Edit View, and a Search Feature NTPCUG Dr. Tom Perkins.
Calendar Browser is a groupware used for booking all kinds of resources within an organization. Calendar Browser is installed on a file server and in a.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to Entity Framework Part 1 Tom Perkins NTPCUG.
ORM Technologies and Entity Framework (EF)
Introduction to MVC Adding Model Classes NTPCUG Tom Perkins, Ph.D.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
For Version 6.0 and later Lattice3D Reporter Tutorial For Version 6.0 and later LATTICE TECHNOLOGY, INC.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Lecture Set 2 Part B – Configuring Visual Studio; Configuration Options and The Help System (scan quickly for future reference)
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
1 Working with MS SQL Server Textbook Chapter 14.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
Teacher’s Assessment Assistant Worksheet Builder Starting the Program
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Associations INFO 2310: Topics in Web Design and Programming.
M1G Introduction to Database Development 5. Doing more with queries.
BIT 286: Web Applications Lecture 10 : Thursday, February 5, 2015 ASP.Net Form Submission.
Gold – Crystal Reports Introductory Course Cortex User Group Meeting New Orleans – 2011.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
1 Getting Started with C++ Part 1 Windows. 2 Objective You will be able to create, compile, and run a very simple C++ program on Windows, using Microsoft.
1 A Very Brief Introduction to Relational Databases.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
SQL Server Reporting Services for.NET Developers Bret Stateham
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com A Quick Overview of ASP.NET Core * aka ASP.NET 5 before.
Introduction to EBSCOhost
Build Data Driven Apps with ASP.NET Core Rachel Appel.
MIS 3200 – C# (C Sharp)
Jim Fawcett CSE686 – Internet Programming Spring 2014
Top 10 Entity Framework Features Every Developer Should Know
Progress Apama Fundamentals
DevOps with ASP.NET Core and Entity Framework Core
ASP.NET Programming with C# and SQL Server First Edition
Unit 9.1 Learning Objectives Data Access in Code
Introduction to Entity Framework
CSE 103 Day 20 Jo is out today; I’m Carl
Lecture 04: Thursday, January 15, 2015
Social Media And Global Computing Managing Databases with MVC
Loops BIS1523 – Lecture 10.
C#: ASP.NET MVC Overview
Unit 8.2 How data can be used Accessing Data in Views
Intro to PHP & Variables
Data Structures and Database Applications Managing Databases with MVC
MVC Framework, in general.
Unit 27 - Web Server Scripting
Principles of report writing
Lecturer: Mukhtar Mohamed Ali “Hakaale”
An Introduction to Entity Framework
GDSS – Digital Signature
NORMA Lab. 5 Duplicating Object Type and Predicate Shapes
Lesson 12.
Developing a Model-View-Controller Component for Joomla
These slides are for reference only. They are not "lecture notes"
Introduction to EBSCOhost
Chapter 18 Finalizing a Database.
Implementing Entity Framework with MVC Jump Start
C# - Razor Pages Db/Scaffolding/Async
Review of Previous Lesson
Tips and tricks in Magento 2 data migration tool
Asp.Net MVC Conventions
Presentation transcript:

BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models

First Steps With Model  first-mvc-app/adding-model.html first-mvc-app/adding-model.html  If you copy and paste the above you’ll get a 404 because of the newline after tutorials/ - delete the space (or the %20, once you’re looking at the 404) and try again.  Setting up the POCO / POJO, managed DB access  Custom connection (to LocalDB, but could be to elsewhere)  Scaffolding: Free CRUD!  Examining the index page 2

EF sets up your DB with “Code First”  “The Entity Framework (often referred to as EF) supports a development paradigm called Code First. Code First allows you to create model objects by writing simple classes. (These are also known as POCO classes, from "plain-old CLR objects.") You can then have the database created on the fly from your classes, which enables a very clean and rapid development workflow.” –Getting Started With MVCGetting Started With MVC  Database first is still an option 3

C# Class definition  Create a plain old CLR object (a normal class) in the Models folder public class Movie { private int numDoors = 4; public int GetNumDoors() { return numDoors; } public void SetNumDoors(int nD) { numDoors = nD; } public int ID { get; set; } public string Title { get; set { if( value == “”) throw new Exception(“blah”); else Title = value; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } }  These are C# Properties  “We'll use the Movie class to represent movies in a database. Each instance of a Movie object will correspond to a row within a database table, and each property of the Movie class will map to a column in the table.” 4

Scaffolding CRUD  CRUD =  Create  Read (examine individually, or list a bunch of them)  Update  Delete  In the Controllers folder, Add  Controller  MVC 6 Controller with views, using Entity Framework  Model Class: Movie  Data context class: ApplicationDBContext  “ Visual Studio creates the following files and folders:  A MoviesController.cs file in the Controllers folder.  A Views\Movies folder.  Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml, and Index.cshtml in the new Views\Movies folder. ” 5

Creating the Database  With luck, the directions behind the above link will work just fine  They didn’t work for me.  The problems were 1.The dnu/dnvm/dnx software wasn’t installed AND/OR 2.The dnu/dnvm/dnx software wasn’t added to the path  For an explanation about what dnu/dnvm/dnx are: Understanding-the-ASP-NET-Runtime Understanding-the-ASP-NET-Runtime 6

Creating the Database: Installing dnvm, etc  Run this to install dnvm (DotNet Version Manager): -NoProfile -ExecutionPolicy unrestricted - Command "&{$Branch='dev';iex ((new-object net.webclient).DownloadString(' ent.com/aspnet/Home/dev/dnvminstall.ps1'))}“  This will copy the files into C:\Users\ \.dnx\bin  Then cd to C:\Users\ \.dnx\bin so that you can use the dnvm program there. 7

Creating the Database: Installing dnvm, etc  Then run dnvm setup  If that doesn’t work try dnvm.cmd setup – it might not work without the.cmd extension  This seems to add the dnvm.cmd program to your path  If not, add it manually  This page has a good explanation – look for the “Via Control Panel” section This page has a good explanation  Add this to your PATH: C:\Users\ \.dnx\bin ; Don’t forget the ; - it’s what separates this entry from the next one  Skip the dnu restore, the do the remainder:  dnvm.cmd use rc1-update1 -p  dnx ef migrations add Initial  dnx ef database update 8

Take it for a spin   Index lists all the objects, in a nifty table  You can ‘Create New’ to add objects  This won’t add unless you get the date format exactly right (and there’s no feedback when you get it wrong  ) 1/1/2016 should work  For each object you can:  View details (a specific page with the same info, but bigger layout)  Edit the object  Delete the object  Ways to use this:  Consulting: generate pages to demonstrate basic stuff to the client  Start here, then go back and modify the HTML / etc so that it actually looks nice 9

Examining the Details page  The ViewData thing was convenient…  … but also error-prone  (there’s no way for the compiler to check that any given field will actually exist when executing the View)  A “Strongly typed” approach would be better  At compile-time we use an object from a specific class  Now compiler can check (in the View) if a given method/property actually exists 10

Examining the Details page public ActionResult Details(int? id) { if (id == null) { return HttpNotFound(); } Movie movie = _context.Movie.Single(m => m.ID == id); if (movie == null) { return HttpNotFound(); } return View(movie); } 11 This is like SELECT * FROM WHERE Note that we’re now passing this specific object to the View.

Examining the Details’ VIEW = "Details";} Details Name For(model => => model.Genre) 12 This will print out the field’s name. (In this case, ‘Genre’ This will print out the field’s value. (For example ‘Comedy’) This where we tell the view which C# type to expect

Examining the Details’ VIEW Name For(model => => model.Genre) 13 These are lambda expressions. Essentially they’re implied, local, “micro” functions.

Lambda Functions (Brief Overview) model => model.Genre  This is actually a very concise function definition.  You can think of this as saying something like: public string MyNewLambdaFnx(Movie model) { return model.Genre; }  UNLIKE a normal method, you can pass a lambda function as a parameter  (i.e., you can treat it kinda like data)  UNLIKE a normal method, these are ‘anonymous’  there’s no actual name for the function  More info at us/library/bb aspxhttps://msdn.microsoft.com/en- us/library/bb aspx 14

Examining the Index page public class MoviesController : Controller { private ApplicationDbContext _context; public MoviesController(ApplicationDbContext context) { _context = context; } // GET: Movies public IActionResult Index() { return View(_context.Movie.ToList()); } 15 This is like ‘SELECT *” This object connects to the database

Examining the Index page  In the controller file: public IActionResult Index() { return View(_context.Movie.ToList()); } In the view IEnumerable // Stuff left (var item in Model) => => item.Title) 16