New HiddenInputAttribute for Templated Helpers in asp.net mvc 2 preview 2
When the new HiddenInputAttribute class is applied to a property, the attribute indicates to the editor template whether a hidden input element should be rendered when editing the model. (The attribute sets an implicit UIHint value of HiddenInput). The DisplayValue property allows you to control whether the value is displayed in editor and display modes. When the attribute is set to false, nothing is displayed (not even the HTML markup that normally surrounds a field).
You might use this attribute in the following scenarios:
· When a view lets users edit the ID of an object and it is necessary to display the value as well as to provide a hidden input element that contains the old ID so that it can be passed back to the controller.
· When a view lets users edit a binary property that should never be displayed, such as a timestamp property. In that case, the value and surrounding HTML markup (such as the label and value) are not displayed.
The following example shows how to use the HiddenInputAttribute class.
public class ProductViewModel {
[HiddenInput] // equivalent to [HiddenInput(DisplayValue=true)]
public int Id { get; set; }
public string Name { get; set; }
[HiddenInput(DisplayValue=false)]
public byte[] TimeStamp { get; set; }
}
When the attribute is set to true (or no parameter is specified), the following occurs:
· In display templates, a label is rendered and the value is displayed to the user.
· In editor templates, a label is rendered and the value is rendered in a hidden input element.
When the attribute is set to false, the following occurs:
· In display templates, nothing is rendered for that field.
· In editor templates, no label is rendered and the value is rendered in a hidden input element.
Posted in: asp.net | Tags: asp.net asp.net mvc mvc 2.0 mvc 2 preview 2 productviewmodel hiddeninput displayvalue