EditorFor and DisplayFor templates in .NET

Make Code 128 in .NET EditorFor and DisplayFor templates

EditorFor and DisplayFor templates
Code 128 Code Set A Printer In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
www.OnBarcode.com
Printing Code 39 Extended In .NET
Using Barcode generation for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.
www.OnBarcode.com
These two different sets of templates are separated into a set of editor and display templates. The editor and display templates are generated from the following methods:
Painting QR Code In .NET
Using Barcode encoder for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
www.OnBarcode.com
Printing PDF417 In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Html.Display("Message") Html.DisplayFor(m => m.Message) Html.DisplayForModel() Html.Editor("UserName") Html.EditorFor(m => m.UserName) Html.EditorForModel()
EAN 13 Encoder In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create European Article Number 13 image in ASP.NET applications.
www.OnBarcode.com
Matrix Generator In .NET
Using Barcode maker for ASP.NET Control to generate, create Matrix image in ASP.NET applications.
www.OnBarcode.com
Although equivalent string-based methods exist for using templates against loosely typed views, we ll prefer to use the expression-based methods to gain the benefits of
Linear 1D Barcode Creation In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Linear Barcode image in ASP.NET applications.
www.OnBarcode.com
USPS PLANET Barcode Encoder In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create Planet image in ASP.NET applications.
www.OnBarcode.com
Using strongly typed templates
Making Code 128 Code Set A In Java
Using Barcode encoder for Java Control to generate, create Code 128C image in Java applications.
www.OnBarcode.com
USS Code 128 Generator In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create USS Code 128 image in ASP.NET applications.
www.OnBarcode.com
using strongly typed views. If our model is simple, we can use the ForModel methods, which enumerate over every member in the model to generate the complete HTML. Because our Change Password page is simple, we can use the EditorForModel method to generate an edit form, as shown in listing 3.13.
Making QR Code ISO/IEC18004 In .NET Framework
Using Barcode creator for .NET Control to generate, create QR Code JIS X 0510 image in .NET framework applications.
www.OnBarcode.com
Painting Data Matrix 2d Barcode In VS .NET
Using Barcode creation for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
Listing 3.13 Using EditorForModel for a simple model
Reading UPC Code In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Recognizing Barcode In Java
Using Barcode Control SDK for BIRT Control to generate, create, read, scan barcode image in BIRT applications.
www.OnBarcode.com
<% using (Html.BeginForm()) { %> <div> <fieldset> Generates edit <legend>Account Information</legend> UI for model <%= Html.EditorForModel() %> <p> <input type="submit" value="Change Password" /> </p> </fieldset> </div> <% } %>
Code 3/9 Printer In None
Using Barcode printer for Font Control to generate, create USS Code 39 image in Font applications.
www.OnBarcode.com
Encode USS Code 39 In Objective-C
Using Barcode maker for iPhone Control to generate, create Code 3/9 image in iPhone applications.
www.OnBarcode.com
This EditorForModel method B loops through all the members on our model for this view, generating the editor templates for each member. Each template generated may be different, depending on the model metadata information on each member. This HTML might suit our needs, but there s only so much you can embed in your view model before you can no longer sanely emit HTML based solely on model metadata. The model for the Change Password screen, shown in listing 3.14, already has validation and label information.
Barcode Printer In Visual Studio .NET
Using Barcode generation for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Code 39 Extended Scanner In Visual Basic .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Listing 3.14 The Change Password model
EAN 128 Creation In Objective-C
Using Barcode drawer for iPad Control to generate, create GTIN - 128 image in iPad applications.
www.OnBarcode.com
Encoding GS1-128 In VS .NET
Using Barcode creation for Reporting Service Control to generate, create GTIN - 128 image in Reporting Service applications.
www.OnBarcode.com
[PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")] public class ChangePasswordModel Requires user to { provide value [Required] [DataType(DataType.Password)] Controls display [DisplayName("Current password")] method of field public string OldPassword { get; set; }
[Required, ValidatePasswordLength [DataType(DataType.Password)] [DisplayName("New password")] public string NewPassword { get; set; } [Required [DataType(DataType.Password)] [DisplayName("Confirm new password")] public string ConfirmPassword { get; set; } }
In this model, we include validation information (the Required attribute B) as well as display information (the DisplayName and DataType attributes C), both of which can be used to influence the final HTML generated in our templates.
View fundamentals
But we may need more control over our HTML than what s allowed or even desired in our model class through metadata information. For example, we might want to surround some of our elements with paragraph tags. For this level of individual control, where we want to lay out individual elements, we can use the EditorFor method, as shown in listing 3.15.
Listing 3.15 Using EditorFor for extra layout control
<p> <%= Html.EditorFor(m => m.OldPassword) %> </p> <p> <%= Html.EditorFor(m => m.NewPassword) %> </p> <p> <%= Html.EditorFor(m => m.ConfirmPassword) %> </p>
Because templates are shared across our site, we may not want to force every editor to include a paragraph tag. For complex forms, we re likely to include organizational elements such as horizontal rules, field sets, and legends to organize our elements, but for simple display and edit models, the EditorForModel and DisplayForModel will likely meet our needs.
Built-in templates
Out of the box, ASP.NET MVC 2 includes a set of built-in templates for both editor and display templates. The included display templates are shown in table 3.2.
Table 3.2 Display templates in ASP.NET MVC 2 Description Renders a link with a mailto URL Conditionally hides the display value Renders the formatted model value Renders the raw content (uses the String template) Combines the model and formatted model value to render a link Loops through an IEnumerable and renders the template for each item Renders a check box for regular Boolean values and a drop-down list for nullable Boolean values Formats the value with two decimals of precision Renders the raw content Loops through all properties of the object and renders the display template for each property
Display template
EmailAddress HiddenInput Html Text Url Collection Boolean Decimal String Object
Using strongly typed templates
With the exception of the Collection and Object templates, each template renders a single value. The Object template iterates through every item in the ModelMetadata.Properties collection (which is, in turn, populated by inspecting the public properties on the item type), and displays the corresponding display template for each item. The Collection template iterates through every item in the model object, displaying the correct display template for each item in the list. The display templates, as you d expect, render display elements to the browser, such as raw text and anchor tags, whereas the editor templates render form elements. The default editor templates are listed in table 3.3.
Table 3.3 Editor templates in ASP.NET MVC 2 Description Uses the HtmlHelper.Hidden extension method to render a <input type="hidden" /> element Uses the HtmlHelper.TextArea extension method to render a multiline input element Uses the HtmlHelper.Password extension method to render a password input element Uses the HtmlHelper.TextBox extension method to render a text input element Loops through an IEnumerable and renders the template for each item, with correct index values Renders a check box for regular Boolean values and a drop-down list for nullable Boolean values Formats the decimal value with two decimals of precision inside a text box Uses the HtmlHelper.TextBox extension method to render a text input element Loops through all properties of the object and renders the editor template for each property
Copyright © OnBarcode.com . All rights reserved.