Model metadata and model template for asp.net MVC: template acquisition and execution strategy

Source: Internet
Author: User
Tags define definition expression split

When we call the HtmlHelper or Htmlhelper<tmodel> template method to render a certain pattern (display mode or edit mode) to a data member of the entire model or model, The template can be found by the Modelmetadata object that represents the model metadata, which is created beforehand. If the template corresponds to a custom partial view, you only need to execute the view, and for the default template, you can get the corresponding HTML directly. This article focuses on the acquisition and execution mechanism of the template, but before that, let's discuss the relationship between Datatypeattribute and templates.

What is the relationship between Datatypeattribute and templates?

Through the "first model metadata" for model metadata definition of the introduction, We know that the data type set by the Datatypeattribute attribute on the target element is ultimately reflected on the Datatypename property of the Modelmetadata object that represents the model metadata. In addition, the data types for some settings, such as Date, time, duration, and currency, are also created with a displayformatattribute applied to modelmetadata. So how does the Modelmetadata datatypename attribute affect the final presentation of the target element?

In fact, the Modelmetadata datatypename attribute is treated as a template name in the process of matching the template, so the following two forms of model type definitions can be considered equivalent. The only difference between the name of the template set through the Uihintattribute attribute and the data type set by the Datatypeattribute attribute is that the former has a higher priority. In other words, if you apply Uihintattribute and Datatypeattribute to the same data member and set the template name and data type to ABC and 123 respectively, the custom template 123 is only used if the template ABC does not exist.

   1:public class Model
2: {
3: [DataType (datatype.html)]
4: Public string Foo {get; set;}
5:
6: [DataType (Datatype.multilinetext)]
7: Public string Bar {get; set;}
8:
9: [DataType (Datatype.url)]
Public string Baz {get; set;}
11:}
12:
13:public class Model
14: {
: [UIHint ("Html")]
Public string Foo {get; set;}
17:
[UIHint ("Multilinetext")]
: Public string Bar {get; set;}
20:
: [UIHint ("Url")]
Public string Baz {get; set;}
23:}

Example Demo: Proving the equivalence between Datatypename and template names

To prove that the data type is set through the Datatypeattribute attribute as a template name in the visual rendering of the target element, let's do a simple example demo. In this example we define a data type triangle that represents a triangle whose properties A, B, and C are a point object that represents the coordinates of the three corners.

1:public class Triangle
2: {
3: [DataType ("Pointinfo")]
4:public point A {get; set;}
5:
6: [DataType ("Pointinfo")]
7:public point B {get; set;}
8:
9: [DataType ("Pointinfo")]
10:public point C {get; set;}
11:}
12:
[TypeConverter (typeof (Pointtypeconverter))]
14:public class Point
15: {
16:public double X {get; set;}
17:public double Y {get; set;}
18:public point (Double x, double y)
19: {
20:this. x = x;
21:this. y = y;
22:}
23:
24:public Static Point Parse (String point)
25: {
26:string[] Split = point. Split (', ');
27:if (split. Length!= 2)
28: {
29:throw New FormatException ("Invalid point expression.");
30:}
31:double x;
32:double y;
33:if (!double. TryParse (Split[0], out x) | |! Double. TryParse (Split[1], out y))
34: {
35:throw New FormatException ("Invalid point expression.");
36:}
37:return new Point (x, y);
38:}
39:}
40:
41:public class Pointtypeconverter:typeconverter
42: {
43:public override bool CanConvertFrom (ITypeDescriptorContext Context,type sourcetype)
44: {
45:return sourcetype = = typeof (String);
46:}
47:
48:public Override Object ConvertFrom (ITypeDescriptorContext Context,cultureinfo Culture, object value)
49: {
50:if (value is String)
51: {
52:return Point.parse (value as String);
53:}
54:return base. ConvertFrom (context, culture, value);
55:}
56:}

For the definition of type triangle and point, there are two points to note: First, the Datatypeattribute attribute is applied to the triangle three A, B, and C attributes and the custom data type is set to Pointinfo (not point); The TypeConverterAttribute attribute is applied on the point type and the TypeConverter type is set to Pointtypeconverter, which supports type conversions from the string. The previous introduction to the complex Type (Complex type) will convert the three properties of triangle from complex type members to simple type members. The three attributes of triangle can be finally rendered, based on the prerequisites for the convenience rules of the object template to the data members.

Now we create a strongly typed partial view with model type point as the template and name it Pointinfo (consistent with the custom data type specified earlier by the Datatypeattribute attribute). We just define the template for the point of display mode, so we put the partial view file in the Views\Shared\DisplayTemplates. As shown in the following code snippet, we display a point object as a (x,y) form.

   1: @model MvcApp.Models.Point
2: (@Model. X, @Model. Y)

Now we create a default homectroller. As shown in the following code snippet, in the default index action method we create a triangle object to render it in the default view.

   1:public class Homecontroller:controller
2: {
3: Public actionresult Index ()
4: {
5: Triangle triangle = new triangle
6: {
7: A = new Point (1,2),
8: B = new Point (2,3),
9: C = new Point (3,4)
: };
One: return View (triangle);
: }
13:}

Here is the definition of the corresponding view, which is a strongly-typed view with model type triangle, we only call the htmlhelper<tmodel> The Displaymodel method will be used as the Triangle object of model to render the display mode.

   1: @model MvcApp.Models.Triangle
2: @Html. Displayformodel ()

Running the Web application will be rendered in the browser as shown in the following illustration, and we can see that the coordinates of the three corners represented by the A, B, and C attributes of the Triangle object we created are rendered in exactly the way we defined the Pointinfo template.

Related Article

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.