#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
- 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
- Fat models, skinny controllers!
- Keep as much business logic in the model as possible.
- If you see your controller getting “fat”, consider offloading some of the logic to the relevant model (or else bad things will start happening!).
- Models should not talk to the views directly (and vice versa).
- Related models provide information to the controller via their association (relation).
- 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);
}
});
}
});
- Scaffold with VS and with MvcScaffolding
- EditorTemplates
- https://github.com/paultyng/Html5EditorTemplates
More Stuff on MVC3 http://asp.net/mvc
@jcreamer898
http://jonathancreamer.freshbrewedcode.com