Exemplo n.º 1
0
        private static MvcHtmlString SmartLabelFor <TModel, TValue>(
            this HtmlHelper <TModel> helper,
            Expression <Func <TModel, TValue> > expression,
            SmartResourceDisplayName resourceDisplayName,
            ModelMetadata metadata,
            bool displayHint      = true,
            object htmlAttributes = null)
        {
            var    result    = new StringBuilder();
            string labelText = null;
            string hint      = null;

            if (resourceDisplayName != null)
            {
                // resolve label display name
                labelText = resourceDisplayName.DisplayName.NullEmpty();
                if (labelText == null)
                {
                    // take reskey as absolute fallback
                    labelText = resourceDisplayName.ResourceKey;
                }

                // resolve hint
                if (displayHint)
                {
                    var langId = EngineContext.Current.Resolve <IWorkContext>().WorkingLanguage.Id;
                    hint = EngineContext.Current.Resolve <ILocalizationService>().GetResource(resourceDisplayName.ResourceKey + ".Hint", langId, false, "", true);
                }
            }

            if (labelText == null)
            {
                labelText = metadata.PropertyName.SplitPascalCase();
            }

            var label = helper.LabelFor(expression, labelText, htmlAttributes);

            if (displayHint)
            {
                result.Append("<div class='ctl-label'>");
                {
                    result.Append(label);
                    if (hint.HasValue())
                    {
                        result.Append(helper.Hint(hint).ToHtmlString());
                    }
                }
                result.Append("</div>");
            }
            else
            {
                result.Append(label);
            }

            return(MvcHtmlString.Create(result.ToString()));
        }
Exemplo n.º 2
0
        public static MvcHtmlString SmartLabelFor <TModel, TValue>(
            this HtmlHelper <TModel> helper,
            Expression <Func <TModel, TValue> > expression,
            string resourceKey,
            bool displayHint      = true,
            object htmlAttributes = null)
        {
            Guard.ArgumentNotEmpty(() => resourceKey);

            var metadata            = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            var resourceDisplayName = new SmartResourceDisplayName(resourceKey, metadata.PropertyName);

            return(SmartLabelFor(helper, expression, resourceDisplayName, metadata, displayHint, htmlAttributes));
        }