Understanding Your Model
What is a Model??
1
2
3
4
5
| public class Contact { public string FirstName { get ; set ; } public string LastName { get ; set ; } public int Age { get ; set ; } } |
What’s Inside Of One?
- Model and ModelType
- Retrieves the value and type of the model itself. Although the model value itself may be null, we may still know the type of the model (for example, we can derive that information from the lambda expression).
- ContainerType and PropertyName
- Retrieves the type of the container object and the name of the property that this value came from. Not all models come from properties, so these may be null.
- Properties
- Retrieves a collection of ModelMetadata objects which describe the properties of the existing model.
- ConvertEmptyStringToNull
- A flag which indicates whether empty strings that are posted back in forms should be converted into NULLs. Default: true
- DataTypeName
- A string which can used to give meta information about the data type (for example, to let you know that this string is actually an e-mail address). Some well-known data type names include “EmailAddress”, “Html”, “Password”, and “Url”. Default: null
- Description
- A long-form textual description of this model. Default: null
- DisplayFormatString
- A format string that will be used when displaying this model value in a template. Default: null
- DisplayName
- The display name of this model value. Used in templates and Html.Label/LabelFor to generate the label text. Default: null
- EditFormatString
- A format string that will be used when editing this model value in a template.Default: null
- HideSurroundingHtml
- A flag which indicates that this field should not have any of its surrounding HTML (for example, a label). Often used when a template will be generating a hidden input. Default: null
- IsComplexType
- A flag which indicates whether the system considers this to be a complex type (and therefore will default to the complex object template rather than the string template). Not user-settable
- IsNullableValueType
- A flag which indicates whether the model is a nullable value type (namely, Nullable<T>). Not user-settable
- IsReadOnly
- A flag which indicates if this value is read-only (for example, because the property does not have a setter). Default: false
- IsRequired
- A flag which indicates if this value is required. Default: true for non-nullable value types; false for all others.
- NullDisplayText
- The text which should be used when attempting to display a null model.Default: null
- ShortDisplayName
- The short display name of this mode value. Intended to be used in the title of tabular list views. If this field is null, then DisplayName should be used.Default: null
- ShowForDisplay
- A flag which indicates if this model should be shown in display mode.Default: true
- ShowForEdit
- A flag which indicates if this model should be shown in edit mode. Default: true
- SimpleDisplayText
- Text which should be shown for this model when summarizing what would otherwise be a complex object display. Default: see below
- TemplateHint
- A string which indicates a hint as to what template should be used for this model. Default: null
- Watermark
- Text that might be displayed as a watermark when editing this model in a text box. Default: null
- GetDisplayName()
- This method can be used to get a display name for the model. If DisplayName is not null, it returns that; then it checks if PropertyName is not null, and if so it returns that; failing all those, it returns ModelType.Name.
- GetValidators()
- This method can be used to retrieve the validators that are applicable for this model. These can be used to either run server-side validation on this model, or to generate the client-side validation rules.
- If the Model is null, return NullDisplayText
- If the type has overridden the value of Model.ToString(), then return that
- If the model has no properties, return String.Empty
- If the model’s first property is null, then return that property’s NullDisplayText value
- Otherwise, return the model’s first property’s ToString() value
- [HiddenInput] (from System.Web.Mvc)
- Applying this attribute will generate a hidden input when editing this model. By default, it will also hide all the surrounding HTML, unless you set the DisplayValue flag to be true; in this case, it will generate both a displayed value (with its surrounding HTML) and the hidden input. In addition to setting HideSurroundHtml, it also sets a TemplateHint of “HiddenInput” (which can be overridden with [UIHint])
- [UIHint] (from System.ComponentModel.DataAnnotations)
- This will set the TemplateHint property with the name of the UI hint. We first look for a PresentationLayer type of “MVC”, and if there isn’t one, look for an empty or null PresentationLayer.
- [DataType] (from System.ComponentModel.DataAnnotations)
- This will set the DataTypeName property.
- [ReadOnly] (from System.ComponentModel)
- This will set the IsReadOnly property. Note that because we use Type descriptors, any property without a public setter will have the [ReadOnly] attribute automatically.
- [DisplayFormat] (from System.ComponentModel.DataAnnotations)
- Setting NullDisplayText on this attribute sets NullDisplayText on model metadata. Setting DataFormatString will set DisplayFormatString on model metadata; if ApplyFormatInEditMode is set to true, then it will also set the EditFormatString on model metadata. Setting ConvertEmptyStringToNull on the attribute will set ConvertEmptyStringToNull on model metadata.
- [ScaffoldColumn] (from System.ComponentModel.DataAnnotations)
- This will set both the ShowForDisplay and ShowForEdit properties.
- [DisplayName] (from System.ComponentModel)
- This will set the DisplayName property.
No comments:
Post a Comment