예제 #1
0
        /// <summary>
        /// Process
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="output">Output</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            //clear the output
            output.SuppressOutput();

            //disabled attribute
            bool.TryParse(IsDisabled, out bool disabled);
            if (disabled)
            {
                var d = new TagHelperAttribute("disabled", "disabled");
                output.Attributes.Add(d);
            }

            //required asterisk
            bool.TryParse(IsRequired, out bool required);
            if (required)
            {
                output.PreElement.SetHtmlContent("<div class='input-group input-group-required'>");
                output.PostElement.SetHtmlContent("<div class=\"input-group-btn\"><span class=\"required\">*</span></div></div>");
            }

            //contextualize IHtmlHelper
            var viewContextAware = _htmlHelper as IViewContextAware;

            viewContextAware?.Contextualize(ViewContext);

            //add form-control class
            bool.TryParse(RenderFormControlClass, out bool renderFormControlClass);
            object htmlAttributes = null;

            if (string.IsNullOrEmpty(RenderFormControlClass) && For.Metadata.ModelType.Name.Equals("String") || renderFormControlClass)
            {
                htmlAttributes = new { @class = "form-control k-input" }
            }
            ;

            var viewEngine  = GetPrivateFieldValue(_htmlHelper, "_viewEngine") as IViewEngine;
            var bufferScope = GetPrivateFieldValue(_htmlHelper, "_bufferScope") as IViewBufferScope;

            if (SelectItems != null)
            {
                if (SelectItems.Any())
                {
                    if (_htmlHelper.ViewData.ContainsKey("SelectList"))
                    {
                        _htmlHelper.ViewData["SelectList"] = SelectItems;
                    }
                    else
                    {
                        _htmlHelper.ViewData.Add("SelectList", SelectItems);
                    }
                }
                else
                {
                    if (_htmlHelper.ViewData.ContainsKey("SelectList"))
                    {
                        _htmlHelper.ViewData["SelectList"] = new List <SelectListItem>();
                    }
                    else
                    {
                        _htmlHelper.ViewData.Add("SelectList", new List <SelectListItem>());
                    }
                }
            }

            var templateBuilder = new TemplateBuilder(
                viewEngine,
                bufferScope,
                _htmlHelper.ViewContext,
                _htmlHelper.ViewData,
                For.ModelExplorer,
                For.Name,
                Template,
                readOnly: false,
                additionalViewData: new { htmlAttributes, postfix = this.Postfix });

            var htmlOutput = templateBuilder.Build();

            output.Content.SetHtmlContent(htmlOutput.RenderHtmlContent());
        }
        public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
        {
            var viewData      = htmlHelper.ViewData;
            var templateInfo  = viewData.TemplateInfo;
            var modelExplorer = viewData.ModelExplorer;

            if (modelExplorer.Model == null)
            {
                return(new HtmlString(modelExplorer.Metadata.NullDisplayText));
            }

            if (templateInfo.TemplateDepth > 1)
            {
                var text = modelExplorer.GetSimpleDisplayText();
                if (modelExplorer.Metadata.HtmlEncode)
                {
                    text = htmlHelper.Encode(text);
                }

                return(new HtmlString(text));
            }

            var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices;
            var viewEngine      = serviceProvider.GetRequiredService <ICompositeViewEngine>();
            var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>();

            var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);

            foreach (var propertyExplorer in modelExplorer.Properties)
            {
                var propertyMetadata = propertyExplorer.Metadata;
                if (!ShouldShow(propertyExplorer, templateInfo))
                {
                    continue;
                }

                var templateBuilder = new TemplateBuilder(
                    viewEngine,
                    viewBufferScope,
                    htmlHelper.ViewContext,
                    htmlHelper.ViewData,
                    propertyExplorer,
                    htmlFieldName: propertyMetadata.PropertyName,
                    templateName: null,
                    readOnly: true,
                    additionalViewData: null);

                var templateBuilderResult = templateBuilder.Build();
                if (!propertyMetadata.HideSurroundingHtml)
                {
                    var label = propertyMetadata.GetDisplayName();
                    if (!string.IsNullOrEmpty(label))
                    {
                        var labelTag = new TagBuilder("div");
                        labelTag.InnerHtml.SetContent(label);
                        labelTag.AddCssClass("display-label");
                        content.AppendLine(labelTag);
                    }

                    var valueDivTag = new TagBuilder("div");
                    valueDivTag.AddCssClass("display-field");
                    valueDivTag.InnerHtml.SetHtmlContent(templateBuilderResult);
                    content.AppendLine(valueDivTag);
                }
                else
                {
                    content.AppendHtml(templateBuilderResult);
                }
            }

            return(content);
        }
        public static IHtmlContent CollectionTemplate(IHtmlHelper htmlHelper)
        {
            var model = htmlHelper.ViewData.Model;

            if (model == null)
            {
                return(HtmlString.Empty);
            }

            var enumerable = model as IEnumerable;

            if (enumerable == null)
            {
                // Only way we could reach here is if user passed templateName: "Collection" to a Display() overload.
                throw new InvalidOperationException($"Collection {model.GetType().FullName}");
            }

            var elementMetadata = htmlHelper.ViewData.ModelMetadata.ElementMetadata;

            Debug.Assert(elementMetadata != null);
            var typeInCollectionIsNullableValueType = elementMetadata.IsNullableValueType;

            var serviceProvider  = htmlHelper.ViewContext.HttpContext.RequestServices;
            var metadataProvider = serviceProvider.GetRequiredService <IModelMetadataProvider>();

            // Use typeof(string) instead of typeof(object) for IEnumerable collections. Neither type is Nullable<T>.
            if (elementMetadata.ModelType == typeof(object))
            {
                elementMetadata = metadataProvider.GetMetadataForType(typeof(string));
            }

            var oldPrefix = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix;

            try
            {
                htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

                var collection      = model as ICollection;
                var result          = collection == null ? new HtmlContentBuilder() : new HtmlContentBuilder(collection.Count);
                var viewEngine      = serviceProvider.GetRequiredService <ICompositeViewEngine>();
                var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>();

                var index = 0;
                foreach (var item in enumerable)
                {
                    var itemMetadata = elementMetadata;
                    if (item != null && !typeInCollectionIsNullableValueType)
                    {
                        itemMetadata = metadataProvider.GetMetadataForType(item.GetType());
                    }

                    var modelExplorer = new ModelExplorer(
                        metadataProvider,
                        container: htmlHelper.ViewData.ModelExplorer,
                        metadata: itemMetadata,
                        model: item);
                    var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", oldPrefix, index++);

                    var templateBuilder = new TemplateBuilder(
                        viewEngine,
                        viewBufferScope,
                        htmlHelper.ViewContext,
                        htmlHelper.ViewData,
                        modelExplorer,
                        htmlFieldName: fieldName,
                        templateName: null,
                        readOnly: true,
                        additionalViewData: null);
                    result.AppendHtml(templateBuilder.Build());
                }

                return(result);
            }
            finally
            {
                htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix;
            }
        }
예제 #4
0
        public static async Task <IHtmlContent> ObjectTemplate(IHtmlHelper htmlHelper)
        {
            var viewData      = htmlHelper.ViewData;
            var templateInfo  = viewData.TemplateInfo;
            var modelExplorer = viewData.ModelExplorer;

            if (templateInfo.TemplateDepth > 1)
            {
                if (modelExplorer.Model == null)
                {
                    return(new HtmlString(modelExplorer.Metadata.NullDisplayText));
                }

                var text = modelExplorer.GetSimpleDisplayText();
                if (modelExplorer.Metadata.HtmlEncode)
                {
                    return(new StringHtmlContent(text));
                }

                return(new HtmlString(text));
            }

            var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices;
            var viewEngine      = serviceProvider.GetRequiredService <ICompositeViewEngine>();
            var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>();

            var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);

            foreach (var propertyExplorer in modelExplorer.Properties)
            {
                var propertyMetadata = propertyExplorer.Metadata;
                if (!ShouldShow(propertyExplorer, templateInfo))
                {
                    continue;
                }

                var templateBuilder = new TemplateBuilder(
                    viewEngine,
                    viewBufferScope,
                    htmlHelper.ViewContext,
                    htmlHelper.ViewData,
                    propertyExplorer,
                    htmlFieldName: propertyMetadata.PropertyName,
                    templateName: null,
                    readOnly: false,
                    additionalViewData: null);

                var templateBuilderResult = await templateBuilder.Build();

                if (!propertyMetadata.HideSurroundingHtml)
                {
                    var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null);
                    using (var writer = new HasContentTextWriter())
                    {
                        label.WriteTo(writer, HtmlEncoder.Default);
                        if (writer.HasContent)
                        {
                            var labelTag = new TagBuilder("div");
                            labelTag.AddCssClass("editor-label");
                            labelTag.InnerHtml.SetHtmlContent(label);
                            content.AppendLine(labelTag);
                        }
                    }

                    var valueDivTag = new TagBuilder("div");
                    valueDivTag.AddCssClass("editor-field");

                    valueDivTag.InnerHtml.AppendHtml(templateBuilderResult);
                    valueDivTag.InnerHtml.AppendHtml(" ");
                    valueDivTag.InnerHtml.AppendHtml(htmlHelper.ValidationMessage(
                                                         propertyMetadata.PropertyName,
                                                         message: null,
                                                         htmlAttributes: null,
                                                         tag: null));

                    content.AppendLine(valueDivTag);
                }
                else
                {
                    content.AppendHtml(templateBuilderResult);
                }
            }

            return(content);
        }