Note that there are some explanatory texts on larger screens.

plurals
  1. POExtension MVC C# LabelFor get all properties
    primarykey
    data
    text
    <p>I'm trying to put together a personalized LabelFor, which take all the properties of the class that are assembled in Data Annotations, and as the property change my Label.</p> <p>example:</p> <pre><code>[Obrigatorio] [Display(Name = "Sexo")] [SexoComTipoPessoa] [NotMapped] public int SexoID { get; set; } </code></pre> <p>This code is ok:</p> <pre><code>[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public class SexoComTipoPessoa : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var property = validationContext.ObjectInstance.GetType().GetProperty("TipoPessoaID"); if (property == null) return new ValidationResult("Propriedade desconhecida: 'TipoPessoaID'"); var propertyValue = property.GetValue(validationContext.ObjectInstance, null); /* Tipo de pessoa 2 - é pessoa juridicia */ if (Convert.ToInt32(propertyValue) == 2) { if (Convert.ToInt32(value) != 3) return new ValidationResult("Para o tipo de pessoa 'Juridíca', deve ser selecionado o sexo 'Não aplicavél'!"); } else if (Convert.ToInt32(propertyValue) == 1) { if (Convert.ToInt32(value) == 3) return new ValidationResult("Para o tipo de pessoa 'Física', deve ser selecionado o sexo 'Masculino' ou 'Feminino'!"); } return ValidationResult.Success; } } </code></pre> <p>This code not ok, when you want the property [Obrigatorio], the label has the property Display (Name = "") and add " * Obrigatório" or [SexoComTipoPessoa] the label has the property Display (Name = "") and add " * Sexo deve ser compativel com tipo de pessoa"</p> <p>public static class LabelExtensions</p> <pre><code>{ public static MvcHtmlString LabelWithTooltip&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; helper, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression) { var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string labelText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(labelText)) return MvcHtmlString.Empty; var label = new TagBuilder("label"); label.Attributes.Add("for", helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); if (!string.IsNullOrEmpty(metaData.Description)) label.Attributes.Add("title", metaData.Description); label.SetInnerText(labelText); return MvcHtmlString.Create(label.ToString()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload