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