As seen in the section on forms, Vici MVC will render HTML controls bound to fields in a class. This is done by adding an attribute to a field, which tells the framework what control to render and how it should look/behave.
You can also create controls explicitly (in code) and render them in the view. For example, to render a text box created from code:
TextControl control = new TextControl("Name"); control.HandlePostback(); // if there is a postback, the // contents of the textbox is // stored in the control's // Value property if (IsPost()) { string postedName = control.Value; // Do something } else { control.Value = "John Doe"; // initial contents } ViewData["NameBox"] = control; // pass it to the view
The view then looks like:
Name: [[NameBox]]
All built-in controls have the following properties in common:
| Property | Type | Description | |
|---|---|---|---|
| Id | String | The XHTML id to render the control with. This should be unique across the rendered page. If not set, the framework will generate a unique id for you. | |
| Name | String | The name of the control, which is required and should only be set by the constructor | |
| Error | Boolean | If set to true, the control will be renderd with the CSS class defined by the CssClassError property. If false, the CSS class defined by CssClass will be used | |
| CssClass | String | The CSS class name to use when rendering the control (if the Error property is false) | |
| CssClassError | String | The CSS class name to use when rendering the control (if the Error property is true) | |
| AutoPost | Boolean | Setting this to true will render the attribute onchange="this.form.submit()" | |
| OnChange | String | Defines a line of javascript to execute when the control is changed on the client. The javascript will be prepended to the javascript generated by the AutoPost property | |
| Enabled | Boolean | Setting this to false causes the control to be renderd with the disabled="disabled" attribute | |
Renders a text box (<input type="text"/>).
Specific properties:
| Property | Type | Description | |
|---|---|---|---|
| Value | String | The text in the textbox | |
| MaxLength | int | The maximum length of the text in the text box (maxlength attribute) | |
| AutoComplete | bool | When set to false, the attribute autocomplete="off" will be rendered | |
| OnKeyPress | string | Adds the specified javascript in the onkeypress attribute | |
| OnKeyDown | string | Adds the specified javascript in the onkeydown attribute | |
| OnKeyUp | string | Adds the specified javascript in the onkeyup attribute | |
| DefaultCssClass | string | (static) Sets the default value for the CssClass property of all TextBoxControl instances | |
| DefaultCssClassError | string | (static) Sets the default value for the CssClassError property of all TextBoxControl instances | |
Renders a dropdown box (<select>).
Specific properties:
| Property | Type | Description | |
|---|---|---|---|
| Value | object | The currently selected value in the list | |
| DataSource | object | A list of objects/values to bind to the control (explained below) | |
| Items | List<DropdownControl.Item> | Contains all items in the list box (explained below) | |
| KeyMember | string | The name of the field or property containing the unique key of the items in the dropdown control. When this property is not set, the object itself will be used as the key (useful when the DataSource is an array) | |
| ValueMember | string | The name of the field or property containing the value (visible to the user) of the items in the dropdown control. When this property is not set, the object itself (converted to a string) will be used to display items in the dropdown list (useful when the DataSource is an array) | |
| ShowBlank | Boolean | If set to true, a blank item will be added to the list (as the first item) | |
| BlankKey | object | The value to store in the Value property when the blank item is selected | |
| BlankValue | object | What to show in the dropdown control as the blank value | |
| ValueFormatString | string | When set, a standard .NET format string will be used to show the items in the list. Use {0} for the key and {1} for the value | |
| DefaultCssClass | string | (static) Sets the default value for the CssClass property of all DropdownControl instances | |
| DefaultCssClassError | string | (static) Sets the default value for the CssClassError property of all DropdownControl instances | |
There are 2 properties available for specifying the items to display in the dropdown control: DataSource and Items. Either or both can be used. The behavior is as follows:
The Items list is of type List<Item>. The Item class has only 2 properties: Key and Value, both of type object. Make sure that you store the right types in the item list.
When handling a postback, it is not necessary to populate the control again. Just retrieve the Value property.
Setting the Value property can be done anytime, even before items are added to the list. The matching of the value with an item from the list is done at render time, not when setting the Value property.
Renders a hidden control (<input type="hidden" />)
There is just one property:
| Property | Type | Description | |
|---|---|---|---|
| Value | string | The value attribute of the control | |
Renders a checkbox control (<input type="checkbox"/>)
Properties:
| Property | Type | Description | |
|---|---|---|---|
| Checked | Boolean | True means the checkbox is checked | |
| OnClick | String | Adds the specified javascript in the onclick attribute | |
Renders a textarea control (<textarea>)
Properties:
| Property | Type | Description | |
|---|---|---|---|
| Value | String | The text in the textbox | |
| Width | Int32 | The width of the text box in characters (it's not recommended to be used, CSS styling is preferred) | |
| Height | Int32 | The height of the text box in lines (it's not recommended to be used, CSS styling is preferred) | |
| OnKeyPress | string | Adds the specified javascript in the onkeypress attribute | |
| OnKeyDown | string | Adds the specified javascript in the onkeydown attribute | |
| OnKeyUp | string | Adds the specified javascript in the onkeyup attribute | |
Renders a checkbox control (<input type="checkbox"/>)
Properties:
| Property | Type | Description | |
|---|---|---|---|
| Checked | Boolean | True means the radio button is checked | |
| Group | String | The group name for this radio button. Only one radio button can be checked per group | |
| OnClick | String | Adds the specified javascript in the onclick attribute | |
For mapping fields to controls in a form, you have to add form field attributes to a field. This is discussed in the [[Projects:Mvc:UserGuide:FormBasics|forms] section.
The following built-in attributes are available:
To be completed
To be completed
To be completed
To be completed
To be completed
To be completed
To be completed
To be completed
To be completed