示例#1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;

            // loop through properties
            foreach (var p in For.ModelExplorer.Properties)
            {
                //  we are not recursing here
                if (p.Metadata.IsComplexType)
                {
                    continue;
                }

                // check the metadata
                if (!Utility.DisplayForEdit(p.Metadata))
                {
                    continue;
                }

                // create a model expression from the explorer
                var f = new ModelExpression($"{p.Container.Metadata.Name }.{ p.Metadata.Name}", p);

                // if we have a hidden field, generate it and continue
                // special case in the case of create - we will have a null object so we dont want hidden Id
                var fa = Utility.GetAttribute <FormAttribute>(p.Metadata);
                if (fa?.Type == FormInputType.Hidden)
                {
                    if (For.Model != null)
                    {
                        output.Content.AppendHtml(tg.GenerateHiddenTagHelper(f));
                    }
                    continue;
                }

                // render
                TagBuilder group = new TagBuilder("div");
                group.Attributes.Add("class", styles.FormGroup);
                group.InnerHtml.AppendHtml(tg.GenerateLabel(f));
                group.InnerHtml.AppendHtml(GenerateContent(f, fa));
                group.InnerHtml.AppendHtml(tg.GenerateValidation(f));
                output.Content.AppendHtml(group);
            }
        }
示例#2
0
        public static bool DisplayForEdit(ModelMetadata meta)
        {
            // check metadata
            if (meta.ShowForEdit == false)
            {
                return(false);
            }

            // read-only fields are skipped
            if (meta.IsReadOnly)
            {
                return(false);
            }

            // check form attribute
            var fa = Utility.GetAttribute <FormAttribute>(meta);

            if (fa != null)
            {
                if (fa.Type == FormInputType.None)
                {
                    return(false);
                }
                if (!fa.DisplayEdit)
                {
                    return(false);
                }
            }

            // check display attribute
            var da = Utility.GetAttribute <DisplayAttribute>(meta);

            if (da != null && da.GetAutoGenerateField() != null)
            {
                if (!da.AutoGenerateField)
                {
                    return(false);
                }
            }

            // if we made it here then display
            return(true);
        }
示例#3
0
        // the ModelExplorer version can be used for dynamic types but then you will need a non-null object
        public static string GetKeyProperty(IEnumerable <ModelExplorer> properties)
        {
            string keyProperty = null;

            foreach (var p in properties)
            {
                // test for key
                var keyAttribute = Utility.GetAttribute <KeyAttribute>(p.Metadata);
                if (keyAttribute != null)
                {
                    keyProperty = p.Metadata.PropertyName;
                }
            }

            // if we do not have a key search for a property called Id
            if (string.IsNullOrWhiteSpace(keyProperty))
            {
                keyProperty = properties.Where(p => p.Metadata.PropertyName.ToLower() == "id").FirstOrDefault()?.Metadata.PropertyName;
            }

            return(keyProperty);
        }
示例#4
0
        public static bool DisplayForView(ModelMetadata p)
        {
            // check meta-data
            if (p.ShowForDisplay == false)
            {
                return(false);
            }

            // check form attribute
            var fa = Utility.GetAttribute <FormAttribute>(p);

            if (fa != null)
            {
                if (fa.Type == FormInputType.Hidden || fa.Type == FormInputType.None)
                {
                    return(false);
                }
                if (!fa.DisplayView)
                {
                    return(false);
                }
            }


            // check display attribute
            var da = Utility.GetAttribute <DisplayAttribute>(p);

            if (da != null && da.GetAutoGenerateField() != null)
            {
                if (!da.AutoGenerateField)
                {
                    return(false);
                }
            }

            // if we made it here then display
            return(true);
        }
示例#5
0
        public static IHtmlContent GetComplexValue(ModelExplorer explorer, IHtmlContent value, ViewContext viewContext, IHtmlHelper htmlHelper, bool renderHtml)
        {
            // check for complex
            if (!explorer.Metadata.IsComplexType)
            {
                return(value);
            }

            // now find the property to display
            var fa = Utility.GetAttribute <FormAttribute>(explorer.Metadata);

            if (fa != null && !string.IsNullOrWhiteSpace(fa.ComplexDisplayProperty))
            {
                var pp = explorer.Properties.Where(pp => pp.Metadata.PropertyName == fa.ComplexDisplayProperty).FirstOrDefault();
                if (pp != null)
                {
                    return(GetFormattedHtml(pp, viewContext, htmlHelper, renderHtml));
                }
            }

            // if we got here then just return the value as-is
            return(value);
        }