![]() ![]() Here’s an example:īased on my experience, I’d suggest that this workaround is the best compromise you can get. There are a few workarounds to this problem.įirst and foremost, you can use the UIHint data annotation to decorate the read-only property. For some reason, this information is just ignored by default templates. The ReadOnly attribute is properly recognized by the metadata provider, and related information is stored in the metadata available for the model. The metadata provider reads information about a given class and exposes that to ASP.NET MVC templated editors. But there’s more to it.ĭata annotations are connected to ASP.NET MVC through the services of a special component known as the metadata provider. You would likely expect that a member with such an annotation be not editable in the form and rendered using the display template for the data type. The Id property of the Article is rendered with a textbox, albeit decorated with the ReadOnly data annotation attribute. A revisited HTML editor formatted as a table. For example, the IsRequired property checks the existence of the Required attribute on the property and enables the “*” symbol close to the input field. Here’s some sample code for an editor template that renders properties within a multi-column HTML table.Ī bit of reflection allows us to determine information about public properties and their metadata. ![]() You just need to write a file named object.cshtml and place it under the folder Views//EditorTemplates. Make the new template handle property the ReadOnly attributeĭisplay and editor helpers come with their own default template, but it is relatively easy to define a custom template.Turn the template into a table-based template with label and input on the same row.Let’s see what you can do to achieve the following two results: However, the metadata information is there, and if you override default templates you have available a lot more meta information that you can consume at your leisure. In ASP.NET MVC, default display and editor helpers simply consume only a few of the possible annotations. The first lesson one can learn about data annotations is that data annotations is one thing and default in ASP.NET MVC is something else. If you test the view, no matter the ReadOnly attribute that decorates the Id field you can happily type into the textbox. The ReadOnly attribute, instead, is ignored. The DisplayName attribute sets the text to be used as the label of the input field. There are a couple of things to notice about the two attributes used in the current version of the class Article. Most of the time, the editor is a textbox but a Boolean property would require a checkbox. You may notice that each public property gets an “editor” in the form of ad hoc markup. Using Html.EditorFor with an annotated view model class. Figure 1 shows the actual result:įIGURE 1. This is one of the predefined editor helpers that gets an object-in the sample, an instance of the Article class-and renders it out within a fixed template. The CSS classes you see in the code snippet that are not prefixed with “gladiator” are all coming from the Twitter’s popular Bootstrap framework.Īs you can see, the core of the form is in the call to Html. ![]() Here’s a sample class annotated with a few attributes.Īs a side note, imagine that this code is an excerpt from a page taken from a personal site-codenamed Gladiator. To start using data annotations, you need to reference the assembly. Data annotations include both rendering and validation attributes. By using data annotations, you decorate your classes with metadata. Using Data Annotationsĭata annotations are attributes that instruct listeners on how to display or edit data. Where you don’t need a special form layout, data annotations can save you a lot of time. These tools include data annotations and template-based HTML helpers conventions include some predefined CSS classes that designers can overwrite to introduce a more appealing look-and-feel. When you’re writing the admin panel for the data in a site, for example, a plain CRUD-inspired set of views are quite good enough.ĪSP.NET MVC offers a few conventions and built-in tools to speed up this aspect of form development. Sometimes, there isn’t any need to spend much time on laying out elements. Although input forms often contain images and require the specific placement of fields, there are plenty of times that they don’t. Input forms play a significant part in the organization of most web views. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |