Expand one of ASP. Net mvc4.0 official tutorials-Add a Chinese display name for model attributes

Source: Internet
Author: User

The official tutorial has been translated. On this basis, we will make some further expansion, with the goal of making localization adjustments and adapting to the current situation of domestic project development. For example, for English-speaking countries, the Library and table field names, model attribute names, and page display names can all be the same, and there is no need to distinguish them, the title bar of the displayed list page or detailed field descriptions on the page are in Chinese, and the fields and model attributes stored in the database table are in English. At present, there is no clear plan, where to write it, and the sources are also miscellaneous. For more information, see the articles written by the predecessors, and find your own solutions. Write them first, finally, sort the data.

This document describes how to add a Chinese display name for model attributes. In the official Tutorial example, you will find that MVC has been written in Chinese, but the Chinese version is not complete. For example, many automatically generated buttons and links are in English. On the list page, the column title bar is an English name. The field description on the detailed page is also in English. Obviously, this presentation cannot be delivered as the final result.

First, let's take a look at the index view of movie generated in the official tutorial and open the views/movie/index. cshtml file. The table header on the list page automatically generated by scaffolding is as follows:

    <tr>        <th>            @Html.DisplayNameFor(model => model.Name)        </th>        <th>            @Html.DisplayNameFor(model => model.Genra)        </th>        <th>            @Html.DisplayNameFor(model => model.Price)        </th>         <th>            @Html.DisplayNameFor(model => model.Rating)        </th>        <th>            @Html.DisplayNameFor(model => model.Date)        </th>        <th></th>    </tr>

The most stupid way is to directly modify the view. For example, change @ html. displaynamefor (model => model. Name) to "movie name ". This can achieve the Chinese effect, but it is very tedious, time-consuming, and error-prone. When there are many columns, column mismatch may occur. To modify the Chinese display name of an attribute, you also need to manually adjust it in multiple crud views. The workload is obvious. It also violates the aforementioned dry principle.

In the previous tutorial, we added the verification attribute for the model. We can be inspired to see if the Chinese display name can be defined only once in the model class and is automatically applied in each view? In fact, MVC has already done this. As you can see from the code above, the scaffolding mechanism is not completely written in the view code, and name/genra is directly written in the column title. Instead, the displaynamefor method of the HTML Assistant class is called, input Model attributes to obtain the display name. You only need to add the Chinese display name attribute for the attribute in the model class. The specific method is to open models/movies. CS and add the display attribute. The Code is as follows:

Public class movie {public int ID {Get; set;} [display (name = "")] public string name {Get; set ;} [display (name = "style")] Public String genra {Get; set;} [display (name = "price")] public decimal price {Get; set ;} [display (name = "release date")] public datetime Date {Get; set;} [display (name = "rating")] Public String rating {Get; set ;}}

That is, add display to the model attribute and set the parameter name to the Chinese name to be displayed. After the modification, execute the generate project operation and run it. You can see that the List page and the add, delete, modify, and modify page take effect automatically. The Chinese field name is displayed.

The official help for viewing the display attribute is as follows:
Namespace:System. componentmodel. dataannotations

Displayattribute class: provides a common feature that allows you to specify localized strings for the types and members of entity segmentation classes.

The name parameter we used above is actually far more than that. I would like to pick a few practical examples below, such as the first autogeneratefield. The official instructions are to get or set a value, this value indicates whether the user interface should be automatically generated to display this field.

In actual business development, some fields are often hidden for background processing and should not be displayed to the user as needed. Therefore, it is added as follows:

[Display (autogeneratefield = false, name = "style")]
Public String genra {Get; set ;}

Result ...... No matter what the problem is, this parameter cannot take effect ...... Genra that sets the autogeneratefield parameter to false. No errors or warnings are reported during compilation or running. However, the running result is always automatically generated in the view ...... I checked the official instructions and searched the results on the Internet based on the keywords, but did not find a reasonable explanation. I tried the description parameter under "Explain", which also had no effect. Therefore, it is estimated that the parameters except name in the system. componentmodel. dataannotations namespace are valid only in winform, but in ASP. net mvc, they are invalid ...... I hope someone familiar with the matter will tell me whether the speculation is correct, what is the real cause, or what is wrong with my usage. Thank you :)

 

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.