Introduction to MVC
Lesson 5 - Views
[WHAT]
- ] VIDEO SUMMARY - notes on this lesson which explains what views do in more detail,
[WHY]
- ] how views are found -
- ] views and models - interaction between views and modes
- ] razor syntax -
- ] HtmlHelper -
- ] Layouts -
[WHERE]
- ] WATCH THE FULL VIDEO
-
[WHEN]
- ] 2014-06-24 - produced
[EXAMPLE]
- [00:00] views
- ] whats the code that determines how views are found
- ] MVC is about convention, rather than compilation, follow a few simple rules && it just works
- ] your views - need to appear inside of the .... /views directory
- ] views typically contains 1 /subdirectory for each controller
- ] /shared directory - which contain views that are share across multiple ...
- ] example - AlbumController would look inside of /views/album directory,
- ] ...
- [05:00] DEMO - view resolutions
- ] Chris shows display View that doesn't exist, goes to browser and attempts to access it
- ] error message - contains listing of all the paths that it searched in order to locate the view
- ] even looks for MVC 1 .aspx, ascx files from legacy MVC 1
- [00:00] how does the view know, what datatype the model will be?
- ] every view has a model property
- ] Type is set by @model declaration
- ] NOTE the casing @model
- ] advanced - possible to NOT type a view to a specific object
- [10:30] Razor syntax
- ] use the @symbol to denote server side code
- ] MVC runtime determines the meaning of @symbol base on context, < a hrelf=mailto:me@home.com> generates the appropriate html
- ] use @@ to specifically declare @
- ] type html whereever you want html, type @ when you want to insert code
- ] @ denotes server side code
- ] remember - the goal of decorating models with attributes
- [16:00] HtmlHelper
- ] einstein once said, dont memorize anything you cna lookup, chris says dont write html(hardcode) for anything you can lookup, let MVC htmlHelper write it for you
- ] Helper uses attributes on model, to determine display names, formatting, input elements
- [17:30] HtmlHelper and Lambdas
- ] @Html.DisplayFor(model => model.name)
- ] MVC uses reflection, reflection isnt evil, its much improved over the years, small performance hit, performance is a feature, reflection is cached, time to market is often more important then MAX perf.
- ] using reflection also keeps things up to date, if code gets changed it wont break
- ] @Helper methods NEED the property model.name NOT the VALUE Model.name
- ] we need the property b/c we need the attributes that are on the property
- ] consider @Html.DisplayFor(m=>m.Name) for readability
- [23:00] Displaying Data
- ] DisplayNameFor - display name attribute, display attribute, name property
- ] DisplayFor - uses display format if applicable
- ] example index view - Html.DisplayNameFor(model => model.Title)
- ] foreach(var item in Model) {
- ] example index view - Html.DisplayFor(modelItem => item.Title) ** because its a list IEnumerable, you want the actual var
- ] exception to the rule - when your looping through an enumaration
- [25:30] Html.ActionLink()
- ] will automatically generate links for you
- ] automatically looks at the routes you have created, generates the links based upon the routes that you have create
- ] good demo of why to use htmlhelper - peoples reaction - i can write html myself, yes you can but, things change, thing long term
- [27:30] accepting input
- ] LabelFor - creates a label element, useful for touch
- ] InputFor - creates an input element, uses HTML5 based on data-type attribute,
- ] demo of ...
- ] dropdown list, how it works, it just works, its baked right in
- ] explanation of the how the dropdown list works
- [33:30] validation
- ] ValidationMessageFor - display error message next to textbox
- ] ValidationSummary - display all error messages in one location , typically used for server side errors, ex TRY save to db result = fail
- [35:00] layouts
- ] organization and consistency shall set you free, when we are creating a web page/app/site we are not giving the user an instruction manual,
- ] keep things consistent, makes it easier for user,
- ] example - standard layout - 3 col - links on outside, content in the center
- ] layout is all about ensuring consistent pages, and they just work !!
- ] layout methods
- ] RenderBody() - renders all view content that is not in a section
- ] RenderSection(name, required:true||false) - allows views to add specific sections, ex scripts, banners, sidebar, use @section name to create section in a view
- [38:30] demo layouts
- *] _ViewStart.cs - is file in root of /views directory, contains code that will be executed prior to any view being displayed,
- ] includes statement to set "layout" for all views
- ] Layout = "/Views/Shared/_Layout.cshtml"
- ] deletes Layout and shows form without the Layout file, looks totally different
- ] Layout can be added later,
- ] reviews content of layout section
- ] layout template includes standard html, for head, navigation, header, body, footer
- ] head includes Styles.Render, Scripts.Render calls, which are more "magic" - that bundles all of your css files, and all your script files into a single download package VS
- ] call to RenderBody() - will add content of view being called to body
- ] call to RenderSection(scripts, required:false) - will add scripts to bottom of page(in this case)
- [41:30] review
- ] use the HtmlHelpers - in the long run will maker you
- ] razor syntax - it just works, thats it
- ] VIEW MODEL jon adds quick demo of creating a viewModel class- in response to chat question
- ] vm is just a class
- ] POINT is IF your models dont cleanly MAP to a view
- ] DONT STRESS - CREATE a viewModel that has the data that you want to pass to your view
- ] see Rachel Appel blog on view models for more info
- ] next lesson is on
[HOW-TO]
- ] # 5050 - my project - guide to asp.net - MVC
- ] # # -
[REFERENCE]
- ] # 5359 - overview series
quiz
x] Which two statements about Views are true?
x] Which statements describe HtmlHelper objects? Choose all that apply.
a= HtmlHelper objects link a Controller with Views.
> HtmlHelper objects determine the input element to use for a property.
> HtmlHelper objects determine the display names for properties.
HtmlHelper objects optimize the HTML output for a View.
> You can create a View that does not reference a Model
x] Which two objects can you pass as a parameter to the HtmlHelper.BeginForm method
- form method, -model, -dbcontext,
> You are using Razor syntax to develop an ASP.NET MVC application. Which symbol indicates code that should run on the server?
--
You need to create a View that displays an invoice date. You must display the date with a specific format. Which HtmlHelper object method should you use?
You need to display information in a specific area of a page. Which method should you use?
results
4/5 PRB ? = which 2 objects can you pass to begin from method