article

Introduction to MVC

Lesson 2 - Models

[WHAT]

  1. ] 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]

  1. ] WATCH THE FULL VIDEO
    1. ] Microsoft MVC intro - Lesson 2 - Models -  by microsoft's Jon Galloway and Christopher Harrison

[WHEN]

  1. ] 2014-06-24 - produced

[EXAMPLE]

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

[HOW-TO]

  1. ] # 5050 - my project - guide to asp.net - MVC
    1. ] # # -

[REFERENCE]

  1. ] # 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

 

 

Details Photos Edit more

Details

ID: 4581

NAME: introduction-MVC-lesson-2

DESCRIPTION: [55:19]VIDEO-SUMMARY Introduction to MVC - Lesson 2 - Models - by Jon Galloway and Christopher Harrison of Microsoft

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: -5

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and