• <bdo id='OC7hS'></bdo><ul id='OC7hS'></ul>
  • <legend id='OC7hS'><style id='OC7hS'><dir id='OC7hS'><q id='OC7hS'></q></dir></style></legend>

  • <small id='OC7hS'></small><noframes id='OC7hS'>

      <tfoot id='OC7hS'></tfoot>
      1. <i id='OC7hS'><tr id='OC7hS'><dt id='OC7hS'><q id='OC7hS'><span id='OC7hS'><b id='OC7hS'><form id='OC7hS'><ins id='OC7hS'></ins><ul id='OC7hS'></ul><sub id='OC7hS'></sub></form><legend id='OC7hS'></legend><bdo id='OC7hS'><pre id='OC7hS'><center id='OC7hS'></center></pre></bdo></b><th id='OC7hS'></th></span></q></dt></tr></i><div id='OC7hS'><tfoot id='OC7hS'></tfoot><dl id='OC7hS'><fieldset id='OC7hS'></fieldset></dl></div>

        从 HTML 帮助器中提取显示名称和描述属性

        Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
        <legend id='dhwLQ'><style id='dhwLQ'><dir id='dhwLQ'><q id='dhwLQ'></q></dir></style></legend>

          <bdo id='dhwLQ'></bdo><ul id='dhwLQ'></ul>

                <tbody id='dhwLQ'></tbody>

                • <tfoot id='dhwLQ'></tfoot>
                • <small id='dhwLQ'></small><noframes id='dhwLQ'>

                  <i id='dhwLQ'><tr id='dhwLQ'><dt id='dhwLQ'><q id='dhwLQ'><span id='dhwLQ'><b id='dhwLQ'><form id='dhwLQ'><ins id='dhwLQ'></ins><ul id='dhwLQ'></ul><sub id='dhwLQ'></sub></form><legend id='dhwLQ'></legend><bdo id='dhwLQ'><pre id='dhwLQ'><center id='dhwLQ'></center></pre></bdo></b><th id='dhwLQ'></th></span></q></dt></tr></i><div id='dhwLQ'><tfoot id='dhwLQ'></tfoot><dl id='dhwLQ'><fieldset id='dhwLQ'></fieldset></dl></div>

                  本文介绍了从 HTML 帮助器中提取显示名称和描述属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在构建一个自定义 HTML.LabelFor 帮助器,如下所示:

                  I am building a custom HTML.LabelFor helper that looks like this :

                  public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip)
                  {
                    var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
                    ...
                  }
                  

                  为了能够获得属性的正确名称,我使用以下代码:

                  To be able to get the proper name for the property I am using the following code :

                  metadata.DisplayName
                  

                  关于我得到的 ModelView 类的属性:

                  And on the property of the ModelView class I got :

                  [DisplayName("Titel")]
                  

                  问题是我还需要一个描述.有一个名为 Display 的属性,它具有名称和描述,但我看不到如何使用上述代码中的元数据变量来提取它?

                  The problem is that I also need a description. There is an Attribute called Display and that has Name and Description but I do not see how to extract this with the metadata variable in the above code?

                  推荐答案

                  免责声明:以下内容仅适用于 ASP.NET MVC 3(如果您使用的是旧版本,请参阅底部的更新)

                  Disclaimer: The following works only with ASP.NET MVC 3 (see the update at the bottom if you are using previous versions)

                  假设以下模型:

                  public class MyViewModel
                  {
                      [Display(Description = "some description", Name = "some name")]
                      public string SomeProperty { get; set; }
                  }
                  

                  还有以下观点:

                  <%= Html.LabelFor(x => x.SomeProperty, true) %>
                  

                  在您的自定义助手中,您可以从元数据中获取此信息:

                  Inside your custom helper you could fetch this information from the metadata:

                  public static MvcHtmlString LabelFor<TModel, TValue>(
                      this HtmlHelper<TModel> self, 
                      Expression<Func<TModel, TValue>> expression, 
                      bool showToolTip
                  )
                  {
                      var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
                      var description = metadata.Description; // will equal "some description"
                      var name = metadata.DisplayName; // will equal "some name"
                      // TODO: do something with the name and the description
                      ...
                  }
                  

                  备注:在同一个模型属性上有 [DisplayName("foo")][Display(Name = "bar")] 是多余的,使用的名称[Display] 属性中的优先级在 metadata.DisplayName 中.

                  Remark: Having [DisplayName("foo")] and [Display(Name = "bar")] on the same model property is redundant and the name used in the [Display] attribute has precedence in metadata.DisplayName.

                  更新:

                  我之前的回答不适用于 ASP.NET MVC 2.0.在 .NET 3.5 中,默认情况下无法使用 DataAnnotations 填充几个属性,而 Description 就是其中之一.要在 ASP.NET MVC 2.0 中实现这一点,您可以使用自定义模型元数据提供程序:

                  My previous answer won't work with ASP.NET MVC 2.0. There are a couples of properties that it is not possible to fill by default with DataAnnotations in .NET 3.5, and Description is one of them. To achieve this in ASP.NET MVC 2.0 you could use a custom model metadata provider:

                  public class DisplayMetaDataProvider : DataAnnotationsModelMetadataProvider
                  {
                      protected override ModelMetadata CreateMetadata(
                          IEnumerable<Attribute> attributes, 
                          Type containerType,
                          Func<object> modelAccessor, 
                          Type modelType, 
                          string propertyName
                      )
                      {
                          var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
                  
                          var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
                          if (displayAttribute != null)
                          {
                              metadata.Description = displayAttribute.Description;
                              metadata.DisplayName = displayAttribute.Name;
                          }
                          return metadata;
                      }
                  }
                  

                  您将在 Application_Start 中注册:

                  protected void Application_Start()
                  {
                      AreaRegistration.RegisterAllAreas();
                      RegisterRoutes(RouteTable.Routes);
                      ModelMetadataProviders.Current = new DisplayMetaDataProvider();
                  }
                  

                  然后助手应该按预期工作:

                  and then the helper should work as expected:

                  public static MvcHtmlString LabelFor<TModel, TValue>(
                      this HtmlHelper<TModel> self, 
                      Expression<Func<TModel, TValue>> expression, 
                      bool showToolTip
                  )
                  {
                      var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
                      var description = metadata.Description; // will equal "some description"
                      var name = metadata.DisplayName; // will equal "some name"
                      // TODO: do something with the name and the description
                      ...
                  }
                  

                  这篇关于从 HTML 帮助器中提取显示名称和描述属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  Getting the attributes of a field using reflection in C#(在 C# 中使用反射获取字段的属性)
                • <i id='QWaUk'><tr id='QWaUk'><dt id='QWaUk'><q id='QWaUk'><span id='QWaUk'><b id='QWaUk'><form id='QWaUk'><ins id='QWaUk'></ins><ul id='QWaUk'></ul><sub id='QWaUk'></sub></form><legend id='QWaUk'></legend><bdo id='QWaUk'><pre id='QWaUk'><center id='QWaUk'></center></pre></bdo></b><th id='QWaUk'></th></span></q></dt></tr></i><div id='QWaUk'><tfoot id='QWaUk'></tfoot><dl id='QWaUk'><fieldset id='QWaUk'></fieldset></dl></div>

                  <small id='QWaUk'></small><noframes id='QWaUk'>

                    <tbody id='QWaUk'></tbody>

                        <legend id='QWaUk'><style id='QWaUk'><dir id='QWaUk'><q id='QWaUk'></q></dir></style></legend>

                          <bdo id='QWaUk'></bdo><ul id='QWaUk'></ul>

                            <tfoot id='QWaUk'></tfoot>