Skip to content

Instantly share code, notes, and snippets.

@jcreamer898
Created March 15, 2012 01:30
Show Gist options
  • Save jcreamer898/2041033 to your computer and use it in GitHub Desktop.
Save jcreamer898/2041033 to your computer and use it in GitHub Desktop.
NashDotNet Lab on MVC3

#MVC3

  • An architectural pattern invented in the late 70's early 80's.
  • Promotes separation of concerns and eases unit testing
  • MVC is great for web apps because of HTTP
    • HTTP requests
    • Retrieve data
    • Return views
  • It is stateless, just like HTTP
    • No postbacks and view state

Bank

  • Customers are views
    • They request stuff, have a little logic (Did I wear pants?)
  • Bankers are models
    • The head honchos that are the only ones allowed into the safe (Db)
  • Tellers are controllers
    • They run all over the place getting money from different bankers

###Things to remember in MVC

  1. Fat models, skinny controllers!
  2. Keep as much business logic in the model as possible.
  3. If you see your controller getting “fat”, consider offloading some of the logic to the relevant model (or else bad things will start happening!).
  4. Models should not talk to the views directly (and vice versa).
  5. Related models provide information to the controller via their association (relation).
  6. It’s quite alright for the views to contain some logic, which deals with the view or presentation.

http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
http://nuts-and-bolts-of-cakephp.com/2009/01/06/another-way-to-think-about-mvc/

##Visual Studio

  • File new project!
  • Requests start at the controller
    • Debug and hit the Index method
    • Do stuff, keep going
  • Show routing and Global.asax
  • Convention is Controller/Action/Index
  • Create a new action method/view
  • Convention is Views/Controller/Action
  • _ViewStart.cshtml
  • _Layout.cshtml

###Razor @{
string awesome = "awesome";
}
@awesome

@for(var i = 0; i < 5; i++){
    <li>Element @i. </li>
}
  • Razor is NOT ANOTHER LANGUAGE...It's merely a way to include C# and Html in the same file
  • The @ symbol just switches between the two, but is smart enough to work with emails etc...
  • Section, Helper, Partial, RenderPartial, Html and Url helpers

###Models

  • Create a model
    • Video store
  • Create a DbContext
  • Generate Db in SqlExpress, look ma no setup!
  • Scaffold a movie
    • List, Create, Update, etc
    • Strongly type the view
    • Create partial view for create and update
  • Model binding and model state
  • Validation attributes
  • Unobtrusive validation
  • jQuery post and model binding

###jQuery

 $("form").submit(function (event) {
     event.preventDefault();
     if ($(this).valid()) {
        $.post('/Ajax/SaveMovie', $(this).serialize(), function (data) {
            if (data.success) {
                var result = $("#result");
                result.html('Saved!');
                setTimeout(function() {
                    result.fadeOut('fast');
                }, 500);
            }
        });
     }
 });

More Stuff on MVC3 http://asp.net/mvc

@jcreamer898
http://jonathancreamer.freshbrewedcode.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment