public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (SubClassType == null)
            {
                throw new ArgumentNullException(SubClassTypeName);
            }

            if (!For.Metadata.ModelType.GetTypeInfo().IsAssignableFrom(SubClassType))
            {
                throw new ArgumentException(string.Format(DefaultMessages.NotASubclass, SubClassType.Name, For.Metadata.ModelType.Name), SubClassTypeName);
            }

            if (For.Model != null && !SubClassType.GetTypeInfo().IsAssignableFrom(For.Model.GetType()))
            {
                output.TagName = string.Empty;
                output.Content.SetHtmlContent(string.Empty);
                return;
            }

            var prefix = DerivedClassesRegister.GetCodeFromType(SubClassType);

            prefix         = combinePrefixes(For.Name, prefix);
            output.TagName = string.Empty;
            string childContent;

            using (var scope = new RenderingScope(For.Model, ViewContext.ViewData.GetFullHtmlFieldName(prefix), ViewContext.ViewData))
            {
                childContent = output.Content.IsModified ? output.Content.GetContent() :
                               (await output.GetChildContentAsync()).GetContent();
            }
            output.Content.SetHtmlContent(childContent);
        }
Exemple #2
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (Transformation == null)
            {
                throw new ArgumentNullException(TransformationName);
            }
            Transformation.Context = ViewContext.HttpContext;
            var type = Transformation.GetType();

            var pres = type.GetMethod("Transform").Invoke(Transformation, new object[] { For.Model });

            var prefix = TransformationsRegister.GetPrefix(type);

            prefix         = combinePrefixes(For.Name, prefix);
            output.TagName = string.Empty;
            string childContent;

            if (string.IsNullOrWhiteSpace(UsePartial))
            {
                using (var scope = new RenderingScope(pres, ViewContext.ViewData.GetFullHtmlFieldName(prefix), ViewContext.ViewData))
                {
                    childContent = output.Content.IsModified ? output.Content.GetContent() :
                                   (await output.GetChildContentAsync()).GetContent();
                }
            }
            else
            {
                var sw   = new StringWriter();
                var prov = ViewContext.HttpContext.RequestServices.GetRequiredService <IModelMetadataProvider>();
                var dict = new ViewDataDictionary(prov, ViewContext.ViewData.ModelState);
                dict.Model = pres;
                dict.TemplateInfo.HtmlFieldPrefix     = ViewContext.ViewData.GetFullHtmlFieldName(prefix);
                dict[TagHelpersProviderContext.Field] = ViewContext.ViewData[TagHelpersProviderContext.Field];
                var newContext = new ViewContext(ViewContext, ViewContext.View, dict, ViewContext.TempData, sw,
                                                 new HtmlHelperOptions {
                    Html5DateRenderingMode          = ViewContext.Html5DateRenderingMode,
                    ClientValidationEnabled         = ViewContext.ClientValidationEnabled,
                    ValidationMessageElement        = ViewContext.ValidationMessageElement,
                    ValidationSummaryMessageElement = ViewContext.ValidationSummaryMessageElement
                });
                childContent = await newContext.RenderPartialView(UsePartial);
            }
            output.Content.SetHtmlContent(childContent);
        }