Introduction to MVC
Lesson 2 - Models
[WHAT]
- ] VIDEO SUMMARY - summary notes on the video, this lesson explains what a model is, the then create a new model and add it to the project
[WHY]
- ]
[WHERE]
- ] WATCH THE FULL VIDEO
-
[WHEN]
- ] 2014-06-24 - produced
[EXAMPLE]
- [00:00] introduction
- ] model - what it is? it is a class
- [02:00] model
- ] a model is just a class
- ] if you know how to create a class, you know how to create a 'model'
- ] you know how to add properties, add methods, ....
- ] there are extra bells and whistles you can do to models to
- ] directory structure has a /models folder -
- ] REMEMBER convention - follow the convention, it just works, web models dont have to exist in the models folder
- ] BUT its 100% up to you, what your models are going to look like, where they are going to live
- [07:30] use add class to add model
- ] note - no project namespace wrapper when use NEW file, class
- ] note - VS TIP - code snippets - type 'prop TAB TAB' to write out "public int myProperty { get, set}
- ] note - VS TIP - type - automatically generate classes for you, mousever new class to get dropdown option, click
- ]app example, Chris creates 2 new classes, ] Artist w/List Albums, ] Album w/Artist
- ] my example, created 2 new classes, ] Questions w/List Answers, ] Answers
- [10:00] a model is just a class, about: creating models
- ] who is going to use it? designed to be the data that the user is going to be interacting with
- ] the model is going to drive your design,
- ] build your design on how your users are expecting the data to look
- ] with MVC you DONT get drag and drop capability, drag a control onto a form, double click to write your code
- ] this is NOT to say MVC wont do a lot for us
- ] input controls are still needed,
- ] its nice to have those auto generated,
- ] we can, its all based on how we build the model
- ] things like - displaying currency, validation, ....
- ] these can all be based on "attributes", attributes "decorate" properties
- ] a model is just a class, but we are looking for MVC to do stuff,
- ] ex - class artist, prop title - should be between x and y characters,
- ] the way we are going to do that ... based on attributes
- ] attributes add info to property that the runtime is going to understand and interpret,
- ] DataType Attributes, Display Attributes, Validation Attributes ( Required, StringLength, RegEx, Compare)
- [14:00] datatype attributes
- ] we can describe our data, helpful for both backend validation and input controls
- ] ex phone table, click email field, see @.com, based upon html5 attributes
- ] ex tell MVC, this is an email address
- *] by putting the info in the class, its centalized, it flows through to views, etc, DRY, centralized at the source of where the info is
- ] when the class is changed it will be automically reflected everywhere
- [18:00] ASIDE ?] re how classes relate to dbs,
- ] why cant you just drag a table in , ] forget db's for the moment, focus on how the data will look for the user
- ] code first - ef, - classes will auto generate db, you can use the designer to ...,
- ] you can drag db table
- ] there is some additional work that needs to be done
- ] how the class gets created is irrelavant
- ] the model is NOT the db, it is a class,
- ] describes the data AND how users
- [20:00] new class Review
- ] VS tip CTRL + . -
- ] VS tip ALT+f12 - previews the code of a class
- ] creates third new class Review
- ] adds review property to "album" class
- [22:00] demonstrating - whats happening( with Attributes )
- ] going to let VS auto setup some things for us ....
- ] Add Controller - Select "MVC 5 controller w/views using EF6", as type of controller, other options include " MVC5 controller w/Read Write, MVC5 controller empty, Web API 2 Controller empty, ..."
- ] doing it with EF b/c wants to make sure it works
- ] Specify Model Class - in this case the new Review class just created
- ] Specify DataContext Class - in this case MVCMusicStore.Models.MVCMusicStoreContext
- > gets ERROR -
- ] adds some more properties to each of the classes
- > gets ERROR - same one, says it can be funny about 1-1 relationships
- ] changing LIST types to "virtual LIST types" ...
- ] jon explains - using code first ef, based on building code based models, "scaffolding" will build out db and views
- [25:30] chris explains FIX PRB
- ] sometimes there are problems with using EF with one-one and many-many relationships, typically using one-many relationships
- ] you can explain to EF framework, when you are using these relationships, just takes a bit of additional work
- ] fix prb on class album - public virtual List<Review> Review {get; set}
- ] on class review - public virtual Album Album {get; set}, public int AlbumID {get;, set;}
- [29:00] small course correction, change of direction
- ] stripping out multiple classes, down to 1 class, with 2 properties
- ] 1 nice thing about scaffolding, easy to delete a bunch of code, b/c its very easy to add it back in
- ] add Controller -
- ] not going to worry about whats happening in the add controller process, this will be looked at in subsequent lesson
- ] albums controller is generated, inheirits from Controller,
- ] points out auto generated files for CRUD
- [31:30] onward with demo, nope
- ] Chris adds back in the removed code
- ] draws a class diagram of the relationship between .... album, artist, review
- ] add Controller - for reviews
- ] mvc will automatically set up urls for you, will demo later how we can go in and change those urls
- ] asp.net site has info on scaffolding in vs 2013,
- ] advanced topics - repositories, patterns, ...., ... developing asp.net mvc4 web applications, more details
- ] more problems with Chris Demo ...
- [40:00] jon tries another, simpler example
- ] super simple demo
- ] no wait, Chris has got his demo working, no ... back to jon
- ] creating an album class, 2 things in it ( id, title)
- *] important - build
- ] add controller - Album
- ] no db involved, very simple class, populated class with data, returning class in view
- ] adding view, using Template (Details), model class "Albums", no data context
- *] scaffolding is the process of creating code from a model class
- ] launch demo - error, expected b/c class is expecting id value to be passed in and none was
- ] note MVC doesnt display id values, you can display them
- ] add id value to url request, success
- [45:00] return to chris demo
- ] apologizes for sideways action, demo is now functioning
- ] shows generated source of email field, input type = text
- ] example of adding Attribute to model
- ] [DateType(DataType.EmailAddress)]
- ] this is the only change made, rebuilds solution, view source
- ] input type is now set to "email"
- ] this is gives you special chars on your keyboard for inputting email addresses, ex @ symbol and .com extension key
- ] ALSO can use attributes to setup ...
- ] date formatting
- ] number formatting
- ] also have the ability to setup your own formatting strings
- +] link in slide for "formatting reference
- [51:30] a bit about validation
- ] check for valid data being input from user
- ] test for valid values, not invalid values
- ] adds Required () attribute
- ] error -
- ] were going to take a break and back in 10 to continue
- [00:00] REVIEW
- ]
- ] next lesson is on ___
[HOW-TO]
- ] # 5050 - my project - guide to asp.net - MVC
- ] # # -
[REFERENCE]
- ] # 5359 - overview series - ] reference ] to-do, ] done
quiz
results 3/5, then 4/5
PRB ? - you add a new controller, what types of controllers can you specify
x] api2, ] Odata, ] EF, ] azuremobilesservice
you add a new controller, which objects does vs add to the project
] db, ] dc, ] views, ] model