Getting "learning notes" from the MVC Model template

Source: Internet
Author: User

There are two types of MVC model templates: An edit mode (@Html. Editorfor ()), a display mode (Html.displayfor ()).

Template acquisition and execution (the following is turned from here):

When we call the HtmlHelper or htmlhelper<tmodel> template method to a data member of the whole model or model in some mode (

Display mode or edit mode), the Modelmetadata object that represents the model metadata can be found by pre-created

The template to be applied. If the template corresponds to a custom partial view, you only need to execute the view, and for the default template, you can directly

to the appropriate HTML.

The parsing of the target template based on the model metadata is the core part of the entire template method execution process, and it is also the focus of this discussion. We aim to

Htmlhelper<tmodel> extension method Displayfor, for example, looks at how the model object obtained through expression expressions is displayed

Pattern is presented.

1:public Static Class Displayextensions

2: {

3:public static mvchtmlstring Displayfor<tmodel, tvalue> (this htmlhelper<tmodel> HTML,

Expression<func<tmodel, tvalue>> expression, string templatename);

4:}

When Displayfor is called, if the model get expression that is represented by the parameter expression is for a property, the property name is

be taken out. The expression is then executed to get an object that is the model, which, together with the property name (if any), is used to represent the model element

The Metadatadata object for the data. The next step is to get a list of the partial template view names based on the Metadatadata object, which

The view names are sorted by priority:

The name of the template passed in as parameter templatename (if not empty).
The value of the Templatehint property of the Metadatadata (if not empty).
The value of the Datatypename property of the Metadatadata (if not empty).
If the true type of the model object is a non-null value type, the type name is the template view name, otherwise the underlying (underlying) type name is used as the modulo

The name of the Board view (for example, type Int32 as the template view name for int?).
If the true type of the model object is a non-complex type, a string template is used (because a non-complex type can implement a string type

Conversion, so it can be converted into a string for rendering).
In the case where the model declaration type is an interface, the collection template is used if the interface inherits from Ienuerable.
Use the object template when the model declaration type is an interface.
If the model declaration type is not an interface type, follow its type inheritance relationship up-to-date knowing the type name as a template

The view name. If the declaration type implements the Ienuerable interface, the last object is replaced with collection.

For the resulting list of representations, all elements are facilitated in order of precedence. For a specific string representing the template view name, the root

The specified path (the display mode and the edit mode are "/displaytemplates/

{templatename} "and"/editortemplates/{templatename} ") to find the view that defines the template. If such a view exists, then

Executes the view directly and returns. If a custom template partial view cannot be found, it is located in the default Template list based on the template name, if

There is a default template for name matching, and the HTML corresponding to the default template is returned directly. If the name in the default Template list is not the same as the specified name

Into the next iteration.

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.