Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        public ResourcesRegistrator.Builder ResourcesRegistrator(ResourcesRegistrator component)
        {
#if MVC
            component.ViewContext = this.HtmlHelper != null ? this.HtmlHelper.ViewContext : null;
#endif
            return(new ResourcesRegistrator.Builder(component));
        }
        public virtual void InitByType(Type type, object data, bool modelOnly = false, string htmlFieldName = "", object additionalViewData = null)
        {
            type = Store.GetEnumerableGenericType(type);

            if (data is IEnumerable)
            {
                data = null;
            }

            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(delegate { return(data); }, type);

            if (metadata != null)
            {
                ResourcesRegistrator registrator = metadata.AdditionalValues.ContainsKey(ClientResourceAttribute.KEY) ? (ResourcesRegistrator)metadata.AdditionalValues[ClientResourceAttribute.KEY] : null;

                if (registrator != null)
                {
                    this.Controls.Add(registrator);
                }

                HtmlHelper html = Ext.Net.X.Builder.HtmlHelper;

                ViewDataDictionary viewData = new ViewDataDictionary(html.ViewDataContainer.ViewData)
                {
                    Model         = data,
                    ModelMetadata = metadata,
                    TemplateInfo  = new TemplateInfo
                    {
                        HtmlFieldPrefix = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)
                    }
                };

                if (additionalViewData != null)
                {
                    foreach (KeyValuePair <string, object> kvp in new RouteValueDictionary(additionalViewData))
                    {
                        viewData[kvp.Key] = kvp.Value;
                    }
                }

                HtmlHelper helper = new HtmlHelper(
                    new ViewContext(html.ViewContext, html.ViewContext.View, viewData, html.ViewContext.TempData, html.ViewContext.Writer),
                    new ViewDataContainer(viewData)
                    );

                foreach (ModelMetadata propertyMetadata in metadata.Properties)
                {
                    ModelFieldAttribute modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null;

                    if (!propertyMetadata.ShowForEdit)
                    {
                        continue;
                    }

                    if (modelAttr == null && this.InitForModelOnly)
                    {
                        continue;
                    }

                    FieldAttribute fieldAttr = propertyMetadata.AdditionalValues.ContainsKey(FieldAttribute.KEY) ? (FieldAttribute)propertyMetadata.AdditionalValues[FieldAttribute.KEY] : null;

                    if ((modelAttr != null && modelAttr.Ignore) || (fieldAttr != null && fieldAttr.Ignore))
                    {
                        continue;
                    }

                    if (propertyMetadata.TemplateHint.IsNotEmpty())
                    {
                        this.Items.Add(new MvcItem(delegate { return(EditorExtensions.Editor(helper, propertyMetadata.PropertyName, propertyMetadata.TemplateHint)); }));
                    }
                    else
                    {
                        Type fieldType = null;

                        if (fieldAttr != null && fieldAttr.FieldType != null)
                        {
                            fieldType = fieldAttr.FieldType;

                            if (!fieldType.IsSubclassOf(typeof(Ext.Net.Field)))
                            {
                                throw new Exception("FieldType must be subclass of Ext.Net.Field");
                            }
                        }
                        else
                        {
                            if (propertyMetadata.IsComplexType)
                            {
                                continue;
                            }

                            if (Store.IsDate(propertyMetadata.ModelType))
                            {
                                fieldType = typeof(Ext.Net.DateField);
                            }
                            else if (Store.IsNumeric(propertyMetadata.ModelType))
                            {
                                fieldType = typeof(Ext.Net.NumberField);
                            }
                            else if (Store.IsBoolean(propertyMetadata.ModelType))
                            {
                                fieldType = typeof(Ext.Net.Checkbox);
                            }
                            else
                            {
                                fieldType = typeof(Ext.Net.TextField);
                            }
                        }

                        Field cmp = (Ext.Net.Field)System.Activator.CreateInstance(fieldType);

                        cmp.ViewContext = helper.ViewContext;
                        cmp.ControlFor  = propertyMetadata.PropertyName;
                        cmp.SetControlFor(propertyMetadata);

                        this.Items.Add(cmp);
                    }
                }
            }
        }