2012-03-09 212 views
6

我创建了属性类来将元数据附加到属性,因此我可以显示表单输入字段的工具提示。IMetadataAware.OnMetadataCreated永远不会调用

HelpAttribute实现IMetadataAware

Public Class HelpAttribute 
    Inherits Attribute 
    Implements System.Web.Mvc.IMetadataAware 

    Public Sub New(text As String) 
     _text = text 
    End Sub 

    Private _text As String 
    Public ReadOnly Property Text As String 
     Get 
      Return _text 
     End Get 
    End Property 

    Public Sub OnMetadataCreated(metadata As System.Web.Mvc.ModelMetadata) Implements System.Web.Mvc.IMetadataAware.OnMetadataCreated 
     metadata.AdditionalValues.Add("HelpText", _text) 
    End Sub 
End Class 

我利用这个元数据在我的扩展方法:

<Extension()> 
Public Function HelpFor(Of TModel, TProperty)(ByVal htmlHelper As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TProperty))) As MvcHtmlString 
    Dim metaData = ModelMetadata.FromLambdaExpression(Of TModel, TProperty)(expression, htmlHelper.ViewData) 

    If metaData.AdditionalValues.ContainsKey("HelpText") Then 
     Dim helpText = metaData.AdditionalValues("HelpText") 
     Return MvcHtmlString.Create(String.Format("<span class=""help""></span><div class=""tooltip"" style=""display: none""><div class=""border-top""></div><div class=""close""><a href=""#"">close</a></div><br class=""clear""><div class=""content"">{1}</div></div>", htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(metaData.PropertyName), helpText, metaData.DisplayName)) 
    End If 

    Return MvcHtmlString.Create(String.Format("<span class=""no_help""></span>", htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(metaData.PropertyName), metaData.DisplayName)) 
End Function 

所以我可以打电话Html.HelpFor任何我的模型的属性,如果有合适的元数据我展示点击(js)显示工具提示的帮助图标。

只要HelpAttribute在与我装饰其属性的类相同的程序集中定义,这一切都可以正常工作。今天我必须将HelpAttribute移动到一个单独的dll(不同的命名空间),所以我这样做了,我引用了该项目,并期望它可以工作。我没有收到任何编译器错误,该应用程序工作正常,但它不显示帮助图标。我调试了代码,发现HelpAttribute的构造函数被调用了不同的属性和正确的文本,但OnMetadataCreated永远不会被调用。有没有人有一个想法,为什么这是如何解决它?

回答

2

我再次自己回答我的问题。显然在SO上发布内容有助于我在头脑中构建问题。当我将HelpAttribute移动到一个单独的类库时,我不得不参考System.Web.MvcIMetadataAware接口。我使用.NET 4.0,并且我自动引用了前段时间为安装测试目的而安装的MVC4。它没有给我任何错误,它只是没有工作。当我将System.web.Mvc更改为版本。 3.0一切正常顺利。

-2
using System; 
    using System.Collections.Generic; 
    using System.Globalization; 
    using System.Web; 
    using System.Web.Mvc; 

    namespace ColorPickerAttributeExample.Attributes 
    { 
     [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 
     public sealed class ColorPickerAttribute : Attribute, IMetadataAware 
     { 
      private const string Template = 
       "$('#{0}').ColorPicker({{onSubmit: function(hsb, hex, rgb, el) {{" + 
       "var self = $(el); self.val(hex);self.ColorPickerHide();}}, onBeforeShow: function() " + 
       "{{$(this).ColorPickerSetColor(this.value);}}}}).bind('keyup', function(){{ $(this).ColorPickerSetColor(this.value); }});"; 

      public const string ColorPicker = "_ColorPicker"; 

      private int _count; 

      // if using IoC container, you could inject this into the attribute 
      internal HttpContextBase Context 
      { 
       get { return new HttpContextWrapper(HttpContext.Current); } 
      } 

      public string Id 
      { 
       get { return "jquery-colorpicker-" + _count; } 
      } 

      #region IMetadataAware Members 

      public void OnMetadataCreated(ModelMetadata metadata) 
      { 
       IList<string> list = Context.Items["Scripts"] as IList<string> ?? new List<string>(); 
       _count = list.Count; 

       metadata.TemplateHint = ColorPicker; 
       metadata.AdditionalValues[ColorPicker] = Id; 

       list.Add(string.Format(CultureInfo.InvariantCulture, Template, Id)); 

       Context.Items["Scripts"] = list; 
      } 

      #endregion 
     } 
    } 



    using ColorPickerAttributeExample.Attributes; 
    using System; 

    namespace ColorPickerAttributeExample.Models 
    { 
     public class HomeModel 
     { 
      [ColorPicker] 
      public string ColorPicker { get; set; } 
      [ColorPicker] 
      public DateTime ColorPicker2 { get; set; } 
     } 
    } 


@model dynamic 

@{ 
    var picker = ViewData.GetModelAttribute<ColorPickerAttribute>(); 
    if (picker != null) 
    { 
     @Html.LabelForModel() 
     @Html.TextBoxFor(m => m, new {id = ViewData.ModelMetadata.AdditionalValues[ColorPickerAttribute.ColorPicker]}) 
    } 
} 
+0

这里发布的代码不属于提出的问题,它只是没有考虑上下文中的样板属性OP的问题。即使代码是有用的 - 它不应该被毫不客气地抛弃,没有解释它的作用以及它如何回答原始问题。 – RobD 2014-03-28 17:50:46

13

这可能不会被调用的另一个原因是如果你有错误的名称空间引用。所以

using System.Web.ModelBinding; 

将编译并不会被撞到,但你应该使用

using System.Web.Mvc; 
+0

谢谢!我不想想象要花多长时间才能弄清楚这一点。 – Gerino 2017-10-18 21:39:11

相关问题