コード例 #1
0
        public async Task <IActionResult> AddAsync([FromServices] IViewLocator viewLocator, [FromQuery] string itemType, [FromQuery] int itemIndex = -1)
        {
            if (itemType == null)
            {
                return(BadRequest());
            }

            if (!Field.ValueContentMetadata.Manager.TryGetMetadata(itemType, out ContentMetadataProvider contentMetadataProvider))
            {
                return(BadRequest());
            }

            if (!contentMetadataProvider.IsInheritedOrEqual(Field.ValueContentMetadata))
            {
                return(BadRequest());
            }

            var newItem = contentMetadataProvider.CreateModelInstance();

            var view = viewLocator.FindView(contentMetadataProvider.ModelType);

            if (view != null && view.DefaultModelData != null)
            {
                newItem = contentMetadataProvider.ConvertDictionaryToContentModel(view.DefaultModelData);
            }

            if (Field.IsListValue)
            {
                if (!(Field.GetModelValue(ContentContext.Content) is IList list))
                {
                    list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(Field.ValueContentMetadata.ModelType));
                }

                if (itemIndex == -1)
                {
                    itemIndex = list.Count;
                }

                list.Insert(itemIndex, newItem);

                Field.SetModelValue(ContentContext.Content, list);
            }
            else
            {
                Field.SetModelValue(ContentContext.Content, newItem);
            }

            await SaveChangesAsync();

            return(await FormValueAsync());
        }
コード例 #2
0
        public async Task RenderAsync(ContentContext contentContext, TextWriter output)
        {
            if (contentContext == null)
            {
                throw new ArgumentNullException(nameof(contentContext));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            var contentView = viewLocator.FindView(contentContext.Explorer.Metadata.ModelType);

            if (contentView == null)
            {
                throw new InvalidOperationException($"Couldn't find content view {contentView.Name}");
            }

            var viewEngineResult = viewEngine.GetView("~/", contentView.Name, false);

            if (!viewEngineResult.Success)
            {
                throw new InvalidOperationException($"Couldn't find view {contentView.Name}");
            }
            var view = viewEngineResult.View;

            var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
            {
                Model = contentContext.Content
            };

            viewData.Add(ViewData_ContentContextKeyName, contentContext);

            var itemRenderingContext = new ViewRenderingContext();

            viewData.Add(ViewData_ViewRenderingContextKeyName, itemRenderingContext);

            using (var contentOutput = new StringWriter())
            {
                var http = contentContext.Services.GetRequiredService <IHttpContextAccessor>();

                var viewContext = new ViewContext
                {
                    HttpContext = httpContextAccessor.HttpContext,
                    ViewData    = viewData,
                    Writer      = contentOutput,
                    RouteData   = new RouteData()
                };

                await view.RenderAsync(viewContext);

                string tagName = "div";
                if (!string.IsNullOrEmpty(itemRenderingContext.HtmlTag))
                {
                    tagName = itemRenderingContext.HtmlTag;
                }

                var tag = new TagBuilder(tagName);
                if (!string.IsNullOrEmpty(itemRenderingContext.CssClass))
                {
                    tag.AddCssClass(itemRenderingContext.CssClass);
                }

                if (!string.IsNullOrEmpty(itemRenderingContext.ScriptName))
                {
                    tag.Attributes.Add("data-content-script", itemRenderingContext.ScriptName);
                }

                if (contentContext.Explorer.IsRoot)
                {
                    tag.Attributes.Add("content-root", string.Empty);
                }
                tag.Attributes.Add("content-type", contentContext.Explorer.Metadata.Name);
                tag.Attributes.Add("content-path", contentContext.Explorer.ModelPath);
                tag.Attributes.Add("content-path-index", contentContext.Explorer.Index.ToString());

                tag.InnerHtml.AppendHtml(contentOutput.ToString());

                tag.WriteTo(output, htmlEncoder);
            }
        }