Extension points in MVC (10) auxiliary methods

Source: Internet
Author: User

The auxiliary methods in MVC are similar to the server controls in ASP. NET and are used to generate specific HTML code. MVC provides three helper classes: htmlhelper for generating HTML elements, ajaxhelper for Ajax processing, and urlhelper for generating URLs. These three helper classes are provided externally through the Ajax, HTML, and URL attributes of viewpage. Because views are inherited from viewpage, we can directly use these three attributes in the view template to call the auxiliary methods.

Ajaxhelper, htmlhelper, and urlhelper are located in system. web. in the MVC namespace, as shown in the following class diagram, these three helper classes only contain some basic methods and attributes, and the implementation of specific auxiliary methods, MVC adopts the extension method. For example, ajaxhelper is implemented through the ajaxextensions static class in the system. Web. MVC. Ajax namespace, and htmlhelper is implemented through a series of static extension classes in the system. Web. MVC. html namespace.

This separation of the specific auxiliary methods into a separate namespace facilitates our extension and substitution of the auxiliary methods:

1. To add a new helper method, we only need to create our own static extension class, and then in the web. add a reference to the namespace of the extension class in the config configuration file: <pages> <namespaces> <add namespace = "namespace of the static extension class"/> </namespace> </pages>

2. to replace all the default auxiliary methods, you only need to replace the specified namespace in the web. config configuration file, such as system. Web. MVC. html, with your own namespace.

Template System

HTML provided by the displayextensions static extension class. display, HTML. displayfor, HTML. the displayformodel auxiliary method is used to automatically generate explicit HTML code based on the model type. The HTML is provided by the editorextensions static extension class. editor, HTML. editorfor, HTML. the editorformodel auxiliary method is used to automatically generate HTML code for editing based on the model type. These are all based on templates: based on the model attribute type, the corresponding templates are extracted from the default template set, and corresponding HTML elements are produced according to the template content.

As shown in the following class chart, you can control the behaviors of the template system through the column features:

When HTML. displayformodel and HTML. editorformodel generate the model display/editing interface, they directly generate HTML elements for each attribute with the type and feature definitions of each model attribute. We can use a subview to define the rendering mode of a specific model. The specific steps are as follows:

1. create two new folders in the project views \ shared directory: displaytemplates and editortemplates. The displaytemplates folder is used to save the template of the model explicit interface (by HTML. displayformodel auxiliary method used), The editortemplates folder is used to save the template of the model editing interface (by HTML. editorformodel ).

2. Add a new view file in the corresponding folder. The name must be exactly the same as the model class name. Select "create subview option ".

3. Consistent with the view template, viewdata and model can be accessed in the subview

After defining the sub-view, the template system first looks for a custom template in the corresponding folder based on the model type name. If yes, the custom template is used. If no, the default template is used.

MVC controls the behavior of the template system through some column features. However, in some cases, the model Source Code cannot be modified, such as by LINQ to SQL or. NET Entity Framework Model class, these classes are automatically generated by tools, we cannot directly modify the source code. Fortunately, these automatically generated classes are defined as partial, so that we can use system. componentmodel. the metadatatypeattribute feature in the dataannotations namespace defines a metadata type related to the model. For example, the product model type is defined as follows:

namespace CnBlogs.TestModel
{
    public partial class Product
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
}
 

We can define related metadata classes:

namespace CnBlogs.TestModel
{
    [MetadataType(typeof(ProductMetadata))] 
    public partial class Product
    {
        private class ProductMetadata
        {
[Displayname ("Product Name")]
            public string Name { get; set; }
[Displayname ("Remarks")]
            public string Description { get; set; }
        }
    }
}
 

Note: The model type must be defined as partial. The metadata definition associated with the model is included in the separated model class, which is an embedded class of the model class. Finally, the metadatatype feature is used to specify the metadata type.

Source code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.