jqueryasp.net-mvcannotationsmetadataxval

How to add help prompt attributes to Business Objects in MVC and display in View


How can I provide some form of Attribute against my Business Objects in my ASP.Net MVC application so that I can pick up the help texts in the View and have them appear as pop-up hover help texts like this:

<%= Html.TextBox("PostalCode", Model.PostalCode, new { 
    title = "Please enter your postal code." })%>

I want to be able to get the 'title' value from my ViewModel which will have picked up the text from an attribute on the Business Object.

In summary, how can I apply help text attributes / metadata on my Business Objects?


Solution

  • Here is how I did it:

    1. Created new Attribute as follows:

      public class HelpPromptAttribute : Attribute
      {
        public HelpPromptAttribute(string text)
        {
            myproperty = text; 
        }
        protected string myproperty;
      
        public string HelpTextKey
        {
            get { return myproperty; }
            set { myproperty = value; }
        }
      }
      
    2. Added attribute to entity property as follows:

      [HelpPrompt("ProjectDescription")]
      [Required(ErrorMessageResourceName = "ProjectDescriptionRequired", ErrorMessageResourceType = typeof(EntityValidationMessages))]
      [StringLength(50, ErrorMessageResourceName = "ProjectDescriptionTooLong", ErrorMessageResourceType = typeof(EntityValidationMessages))]
      public string ProjectDescription { get; set; }
      
    3. Added Extension Method to Entities as follows:

      public static class EntityBOExtensions
      {
        public static string PropertyHelp(this object objectBO, string PropertyName)
        {
            Type type = objectBO.GetType();
      
      
            foreach (PropertyInfo pInfo in type.GetProperties())
            {
                if (string.Compare(pInfo.Name, PropertyName) == 0)
                {
                    foreach (Attribute attr in Attribute.GetCustomAttributes(pInfo))
                    {
                        if (attr.GetType() == typeof(HelpPromptAttribute))
                        {
                            string key = ((HelpPromptAttribute)attr).HelpTextKey;
                            if (!string.IsNullOrEmpty(key))
                                return EntityBOHelp.ResourceManager.GetString(key);
                        }
                    }
                }
            }
            return null;
        }
      }
      
    4. Added a HtmlHelper (simple) as follows:

      public static string LocalisedTextBoxWithHelp(this HtmlHelper helper, string name, object value, string helptext)
      {
          string textbox = helper.TextBox(name, value, new { helpprompt = helptext });
          return textbox;
      }
      
    5. And finally used the folowing markup in the View:

       <%= Html.LocalisedTextBoxWithHelp("project.ProjectDescription", Model.ProjectDescription, Model.PropertyHelp("ProjectDescription"))%>
      

    This does the job although it needs refinement. ;)