Further exploring the View aspect of MVC Jim Warren, COMPSCI 280 S2 2014 Enterprise Software Development.

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Tutorial 6 Creating a Web Form
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
Web Design with Cascading Style Sheet Lan Vu. Overview Introduction to CSS Designing CSS Using Visual Studio to create CSS Using template for web design.
The Web Warrior Guide to Web Design Technologies
Intermediate Level Course. Text Format The text styles, bold, italics, underlining, superscript and subscript, can be easily added to selected text. Text.
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Tutorial 6 Working with Web Forms
Computing Concepts Advanced HTML: Tables and Forms.
There is a certain way that an HTML file should be set up. The HTML section declares a beginning and an ending. Within the HTML, there should be a HEAD.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Creating Web Page Forms
HTML Tables and Forms Creating Web Pages with HTML CIS 133 Web Programming Concepts 1.
CST JavaScript Validating Form Data with JavaScript.
WDV 331 Dreamweaver Applications Templates Dreamweaver CS6 Chapter 19.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
XP Tutorial 6New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Creating Web Page Forms Designing a Product Registration Form Tutorial.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Lesson 13: Building Web Forms Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 12 – Microsoft FrontPage Express Outline 12.1Introduction 12.2Microsoft FrontPage Express.
Copyright © Texas Education Agency, All rights reserved. 1 Web Technologies Website Development with Dreamweaver.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Neal Stublen Improvements  New form elements  Assist with validation  Consistency across sites  Changes reduce the need for common.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Web Technologies Website Development Trade & Industrial Education
HTML II. Factors to consider in designing a website. Organizing your files. HTML Tables. Unordered Lists. Ordered Lists. HTML Forms. Learning Objectives.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Website Development with Dreamweaver
NASRULLAH KHAN.  Lecturer : Nasrullah   Website :
PHP meets MySQL.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
Microsoft Visual Basic 2005 CHAPTER 7 Creating Web Applications.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
HTML Forms.
HTML: Hyptertext Markup Language Doman’s Sections.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 7.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
CIS234A- Lecture 7 Instructor Greg D’Andrea. Tables A table can be displayed on a Web page either in a text or graphical format. A text table: – contains.
1 Building FORMS In When a visitor enters information into a web form displayed in a web browser and clicks the submit button, the information is sent.
Introduction to JavaScript CS101 Introduction to Computing.
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
Web Technologies Lecture 3 Web forms. HTML5 forms A component of a webpage that has form controls – Text fields – Buttons – Checkboxes – Range controls.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Chapter 27 Getting “Web-ified” (Web Applications) Clearly Visual Basic: Programming with Visual Basic nd Edition.
20-753: Fundamentals of Web Programming 1 Lecture 6: Advanced HTML Fundamentals of Web Programming Lecture 6: Advanced HTML.
Positioning Objects with CSS and Tables
1 HTML Forms
Department of Computer Science, Florida State University CGS 3066: Web Programming and Design Spring Forms, HTML5 layout.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
CHAPTER TWO HTML TAGS. 1.Basic HTML Tags 1.1 HTML: Hypertext Markup Language  HTML stands for Hypertext Markup Language.  It is the markup language.
Tutorial 6 Creating a Web Form
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
XP Tutorial 6New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
CSCI 1720 W3.CSS – Part 1 East Tennessee State University Department of Computing CSCI 1720 Intermediate Web Design.
Unit I: Collecting Data with Forms
Web Development Using ASP .NET
Presentation transcript:

Further exploring the View aspect of MVC Jim Warren, COMPSCI 280 S Enterprise Software Development

Today’s learning objectives  To be able to develop Views in Razor/HTML syntax to suit application needs for MVC ASP.NET applications  Including development of forms for interactive data collection from the user  This requires adequate understanding of the interaction of the MVC elements and HTML, CSS and JavaScript libraries COMPSCI 2802

ASP, ASP.NET  Properly, what we’re learning to do here is create ASP.NET MVC 4 Web Applications using the Razor view syntax  ASP.NET is an open source server-side Web application framework designed for Web development to produce dynamic Web pages  Developed by Microsoft to allow programmers to build dynamic websites, Web applications and Web services  By being ‘server-side’, we mean the server does most of the work using the code we write and serves up a processed result to the user’s browser  Unlike, say, Javascript or a Java Applet, which can be processed client-side by the browser software itself  ASP.NET is the successor to Active Server Pages (ASP) technology, which was Microsoft’s first server-side script engine for dynamically generated web pages (originally release around 1996) Handout 04COMPSCI 2803

Razor syntax  Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or Visual Basic.NET programming languages  It’s relatively new (released with VS 2010 in January 2011)  Starts character as compared to the brackets used for ASP and ASP.NET  Designed to give smoother integration of the server-side code into the HTML  E.g. the implicit ending to a razor expression within HTML  Intelli-sense  And no new syntax to learn – it’s C# or VB  Which doesn’t mean that you don’t need to get used to a lot of new objects and the coding style – but it’s not another ‘language’ per se Handout 04COMPSCI 2804

 Note that we explicitly chose the Razor rather than ASPX view syntax when we created the MVC project  Well, it was the default Handout 04COMPSCI 2805

Creating the Edit/Update form  We’re aiming for something like this screen  We need to create a new.cshtml page  We need to populate it with an HTML form containing HTML fields for data entry and an HTML submit button (labelled ‘Save it’ in this example)  It also has that ‘Back to List’ hyperlink to return to the index/home page Handout 04COMPSCI 2806

Making the new page in the View  In the VS Solution Explorer  Right-click the Home subdirectory of the View  And Add a new View  Call it ‘Edit’  The defaults are fine and should give you something like: Handout 04COMPSCI ViewBag.Title = "Edit"; } Edit

Making this view into what we want  First, I’ll need to associate it with the Model:  Then I get into the real ‘payload’ of the form: Handout 04COMPSCI ViewBag.Title @using (Html.BeginForm()) Employee The => => => => model.Surname)... Actually I could’ve gotten this auto- generated if I’d ticked ‘Create a strongly typed view’ when adding the class (I’d then get a list of classes in the Model) I can put in various content using Razor expression, like items from the ViewBag into the HTML heading, or accessing the system time Then I create this Razor code block with the expressions in it

HTML Forms  Remember, the Razor code is there to create HTML  So what sort of HTML am I aiming for?  Well, a simple HTML form might look like this (from Handout 04COMPSCI 2809 First name: Last name: The action points to the server page that will ‘catch’ the result; if the method is “get” then the user can see the values they filled out as parameters – alternatively, the “post” method doesn’t show the parameters tags say to render input fields for the user. The ‘type’ attribute says what sort of data it is A type of “submit” indicates to render a special button; when the user presses it, the browser sends the form data to the action URL

HTML5 and JavaScript/jQuery  HTML5 (the latest version) adds lots of input field types  Color, date, datetime, , month, number, range, search, tel, time, url, week  These give the browser greater information about the input expectation (as compared to, say, “text”) and thus opportunity to give helpful input controls  E.g. to provide date and colour pickers, or to customize the keyboard on a mobile touch display for ease of entering an or URL  HTML5 also greatly expands the input tag attributes for content validation  Including: min, max, step, required (see  This reduces the need for the application developer to provide custom client side scripting (generally in the Javascript language) to support valid data entry Handout 04COMPSCI 28010

HTML5 and Javascript/jQuery (contd.)  Before HTML5, to get a good user experience on an HTML form you’d typically use heaps of Javascript  jQuery is a very widely used Javascript library to simplify client-side scripting  It ships with Visual Studio  You’ll find it in numerous.js files by looking under Scripts in Solution Explorer in your MVC project  Some gets included in a default View’s HTML in MVC 4, etc.  Nonetheless, the trend with HTML5 is for the HTML to let the browser bear more of the validation and less to need to be specified with Javascript Handout 04COMPSCI 28011

More on tidy form HTML  Good to use the tag to produce captions for your input fields  Not strictly necessary, but gives the browser more information  Using the label tags let the browser configure this radio button group, with the bonus that the clicking the ‘Male’ and ‘Female’ text will make a selection Handout 04COMPSCI Male Female

Back to our Edit/Update screen  What HTML does that Razor from slide 8 give us?  Inspecting the Surname input in Chrome gives: Handout 04COMPSCI Note the id parameter to the Edit handler in the Home controller is passed on the action URL, but the method is ‘post’ for the return payload of an updated instance of the Employee class object The primary key went in as a hidden field (we want to have the value, but not to edit it), but VS still mechanically added data validation for it The ‘data-’ properties aren’t interpreted by the browser, but instead are used by jquery.validate.unobtrusive.js to dynamically make the ‘unobtrusive’ data-val-required message visible when the field is blank

What about the date?  Let’s go back and look at an revision of our Model Handout 04COMPSCI [Table("employee")] public class Employee { [Key] public int IDnum { get; set; } [Required] public string Surname { get; set; } [Display(Name="Given Names")] public string GivenNames { get; set; } [Display(Name = "Date of Birth")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime DateOfBirth { get; set; } } With DateOfBirth, we use the decorators to say to treat its type as Date (i.e. we aren’t interested in the time-of-day component). The specific “yyyy-MM- dd” format is required so that Chrome will pick up the current value for editing (even though Chrome then shows it in a different culture-sensitive format for the actual editing in the browser!). The [Required] decorator caused VS to include that jQuery validation in the HTML (as per previous slide) The Name parameter on [Display] decides the field’s label for the HTML form Cap “MM” for month because lower “mm” is for minutes in DateTime format strings

The rest of Edit.cshtml Handout 04COMPSCI => => => model.DateOfBirth) to List", creates data validation scripts and some internationalisation stuff as Scripts } Actually the Razor for DateOfBirth (and GivenNames, too) is really similar to the Razor for Surname. The difference is conveyed by the Model Includes the jQuery Razor’s block comment chars Link back to home page – essentially canceling the update

Date HTML Reasonably pretty date picker in Chrome Handout 04COMPSCI Note that yyyy-MM-dd format for the initial value Similar use of ‘data-’ attributes to communicate to the jQuery as we saw to implement Surname’s validation and message

What about the ‘pretty’ home page we saw last lecture? Handout 04COMPSCI Custom logo Reasonable alignment choices and padding between columns Striped rows to aid reading across ‘Relevant’ content for the static part of the page

Back to Index.cshtml Handout 04COMPSCI grid.Columns( grid.Column("IDnum","Employee ID",style: "centercol"), grid.Column("Surname","Last Name"), grid.Column("GivenNames","Given of Birth",style:"centercol"), grid.Column(header: "Edit", format: (item) => Html.ActionLink("Edit", "Edit", new { id=item.IDnum })) ),rowStyle: "oddrow") Note: All edits must be approved by Human Resources (HR). The ‘style’ parameter on the Column declaration just literally added a ‘class’ attribute to the HTML And the “oddrow” rowStyle parameter on the entire.GetHtml method did much the same thing, but this time the class declaration is only on every other

Back to Index.cshtml (contd.) Handout 04COMPSCI grid.Columns( grid.Column("IDnum","Employee ID",style: "centercol"), grid.Column("Surname","Last Name"), grid.Column("GivenNames","Given of Birth",style:"centercol"), grid.Column(header: "Edit", format: (item) => Html.ActionLink("Edit", "Edit", new { id=item.IDnum })) ),rowStyle: "oddrow") Note: All edits must be approved by Human Resources (HR). Note that the right-hand side of Chrome’s inspect element shows the CSS that matches the item. For the table padding we just added some padding to the existing entry for ‘th’ (tag of a table header cell) in the Site.css file (under Content in VS Solution Explorer) Also in Site.css we need to define what we want the style and rowStyle to mean.centercol { text-align: center;}.oddrow { background-color: #a6dbed;} (the dot in CSS means it applies to anything of that class) 1. 2.

The logo  The top-left “your logo here” item isn’t in Index.html, it’s part of the site’s master template  _Layout.cshtml under Views/Shared in Solution Explorer  But as it turns out, the.cshtml declares that area with class=“site- title” and the best way to put an image there is to edit (yet again!) the Site.css file Handout 04 COMPSCI site-title a,.site-title a:hover,.site-title a:active { background: url( no-repeat top left; display: block; width: 150px; height: 80px; text-indent: -9999px; /* hides the link text */ color: #c8c8c8; outline: none; text-decoration: none; }

A bit more on that DataOfBirth column  We don’t want the time on the DateOf Birth so we can use a Razor expression  Again we see the WebGrid’s magic ‘item’ object  item is referenced in a format parameter expression  ‘format:’ is followed and ends at  The content inside gets put in each table row at that column  In this case I mainly wanted to put in a Razor expression so I use the inert tag  Also note the approach of using name, rather than position, to indicate the optional arguments (format, header)  See Handout 04COMPSCI of Birth",style:"centercol"),...

Where we’re up to  You should now feel you can bend the default MVC application to your needs  Now…  You should be very focused on Assignment 2  Subsequent lectures will fill in further techniques and more in- depth explanation of some of the C# language features Handout 04COMPSCI 28022