public void RenderLabel(HtmlTextWriter textWriter, FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            var bootStrapBuilderParameters = (BootStrapBuilderParameters)formBuilderParameters;
            // if there is a Name specified in the DisplayAttribute use it , other wise use the property name 
            var displayName = formElement.ControlSpecs.ControlName.SpacePascal();

            if (!String.IsNullOrEmpty(formElement.ControlSpecs.Name))
            {
                displayName = formElement.ControlSpecs.Name;
            }

            // Label
            if (bootStrapBuilderParameters.FormType == BootstrapFormType.Horizontal)
            {
                textWriter.AddAttribute(HtmlTextWriterAttribute.Class, "col-lg-3 control-label");
            }
            else
            {
                textWriter.AddAttribute(HtmlTextWriterAttribute.Class, "control-label");
            }
            textWriter.AddAttribute(HtmlTextWriterAttribute.For, formElement.ControlSpecs.ControlName);
            textWriter.RenderBeginTag(HtmlTextWriterTag.Label);
            textWriter.Write(displayName);
            textWriter.RenderEndTag(); // label
        }
        public MvcHtmlString BuildElement(BootstrapFormType formType, FormElement formElement)
        {
            var sb = new StringBuilder();
            var formBuilderParameters = new BuilderParameters(formType);

            sb.Append(this.formBuilder.RenderElement(formElement, formBuilderParameters));

            return new MvcHtmlString(sb.ToString());
        }
Beispiel #3
0
        public string EndElementContainer(FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            var sb = new StringBuilder();
            var stringWriter = new StringWriter(sb);
            var textWriter = new HtmlTextWriter(stringWriter);

            ElementContainer.End(textWriter);
            return sb.ToString();
        }
        public TagBuilder RenderElement(FormElement formElement)
        {
            TagBuilder elementTagBuilder = null;

            switch (formElement.ControlSpecs.ElementType)
            {
                case ElementType.Text:
                    elementTagBuilder = this.RenderTextBox(formElement);
                    break;
                case ElementType.Hidden:
                    elementTagBuilder = this.RenderHidden(formElement);
                    break;
                case ElementType.TextArea:
                    elementTagBuilder = this.RenderTextArea(formElement);
                    break;
                case ElementType.Password:
                    elementTagBuilder = this.RenderPassword(formElement);
                    break;
                case ElementType.DateTime:
                    elementTagBuilder = this.RenderDateTime(formElement);
                    break;
                case ElementType.FloatingPointNumber:
                    elementTagBuilder = this.RenderFloatingPointNumber(formElement);
                    break;
                case ElementType.WholeNumber:
                    elementTagBuilder = this.RenderWholeNumber(formElement);
                    break;
                case ElementType.Time:
                    elementTagBuilder = this.RenderDateTime(formElement);
                    break;
                case ElementType.CheckBox:
                    elementTagBuilder = this.RenderBoolean(formElement);
                    break;
                case ElementType.Enum:
                    elementTagBuilder = this.RenderEnum(formElement);
                    break;
                case ElementType.List:
                    elementTagBuilder = this.RenderDropDownList(formElement);
                    break;
                case ElementType.ListBox:
                    elementTagBuilder = this.RenderListBox(formElement);
                    break;
                case ElementType.StaticText:
                    elementTagBuilder = this.RenderStaticText(formElement);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (elementTagBuilder != null)
            {
                elementTagBuilder.AddCssClass("form-control");
            }

            return elementTagBuilder;
        }
 private TagBuilder RenderHidden(FormElement formElement)
 {
     var value = formElement.FieldValue;
     var tagbuilder = new TagBuilder("input");
     tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
     tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
     tagbuilder.MergeAttribute(HtmlAtrributes.Type, "hidden");
     var hiddenValue = (value == null) ? String.Empty : value.ToString();
     tagbuilder.MergeAttribute(HtmlAtrributes.Value, hiddenValue);
     return tagbuilder;
 }
        public void BeginElementContainer(HtmlTextWriter contextWriter, FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            this.elementContainerWriter = contextWriter;
            this.elementContainerWriter.AddAttribute(HtmlTextWriterAttribute.Class, "form-group");

            if (formElement != null && formElement.HasErrors)
            {
                elementContainerWriter.AddAttribute(HtmlTextWriterAttribute.Class, Foundation.Web.Configurations.WebConfigurations.HasErrorsCssClass);
            }

            elementContainerWriter.RenderBeginTag(HtmlTextWriterTag.Div); // div (ElementType-Group)
        }
        public static void Begin(HtmlTextWriter textWriter, FormElement formElement)
        {
            
            textWriter.AddAttribute(HtmlTextWriterAttribute.Class, "form-group");

            if (formElement != null && formElement.HasErrors)
            {
                textWriter.AddAttribute(HtmlTextWriterAttribute.Class, Foundation.Web.Configurations.WebConfigurations.HasErrorsCssClass);
            }

            textWriter.RenderBeginTag(HtmlTextWriterTag.Div); // div (ElementType-Group)
        }
        private TagBuilder RenderStaticText(FormElement formElement)
        {
            var value = formElement.FieldValue;
            var tagbuilder = new TagBuilder("p");

            tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
            tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
            tagbuilder.MergeAttribute(HtmlAtrributes.Class, "form-control-static");
            tagbuilder.InnerHtml = Convert.ToString(value);

            return tagbuilder;

        }
        public void BeginControlContainer(HtmlTextWriter textWriter, FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            var bootStrapBuilderParameters = (BootStrapBuilderParameters) formBuilderParameters;
            var formType = bootStrapBuilderParameters.FormType;
            if (formType == BootstrapFormType.Horizontal)
            {
                // Controls Div
                textWriter.AddAttribute(HtmlTextWriterAttribute.Class, "col-lg-9");
            }

            textWriter.RenderBeginTag(HtmlTextWriterTag.Div);
            
        }
        public TagBuilder RenderElement(FormElement formElement)
        {
            TagBuilder elementTagBuilder = null;

            if (formElement.ControlSpecs.ElementType != ElementType.Hidden)
            {
                elementTagBuilder = this.RenderStaticText(formElement);
            }
            else
            {
                elementTagBuilder = this.RenderHidden(formElement);
            }

            return elementTagBuilder;
        }
        public virtual TagBuilder RenderBoolean(FormElement formElement)
        {
            var tagbuilder = new TagBuilder("input");
            var value = formElement.FieldValue;
            
            if (Convert.ToBoolean(value))
            {
                tagbuilder.MergeAttribute(HtmlAtrributes.Checked, "checked");
            }

            tagbuilder.MergeAttribute(HtmlAtrributes.Type, "checkbox");
            tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
            tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
            tagbuilder.MergeAttribute(HtmlAtrributes.Value, "true");

            return tagbuilder;
        }
Beispiel #12
0
        public string RenderElement(FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            var sb = new StringBuilder();

            sb.Append(this.layoutBuilder.BeginElementContainer(formElement, formBuilderParameters));
            {
                sb.Append(this.layoutBuilder.RenderLabel(formElement, formBuilderParameters));
                sb.Append(this.layoutBuilder.BeginControlContainer(formElement, formBuilderParameters));
                {
                    var elementBlock = this.elementGenerator.RenderElement(formElement);

                    elementBlock.MergeAttributes(formElement.ValidationAttributes);

                    sb.Append(elementBlock);
                }
                sb.Append(this.layoutBuilder.EndControlContainer(formElement, formBuilderParameters));
            }
            sb.Append(this.layoutBuilder.EndElementContainer(formElement, formBuilderParameters));

            return(sb.ToString());
        }
Beispiel #13
0
        public string RenderElement(FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            var sb = new StringBuilder();
            
            sb.Append(this.layoutBuilder.BeginElementContainer(formElement, formBuilderParameters));
            {
                sb.Append(this.layoutBuilder.RenderLabel(formElement, formBuilderParameters));
                sb.Append(this.layoutBuilder.BeginControlContainer(formElement, formBuilderParameters));
                {
                    var elementBlock = this.elementGenerator.RenderElement(formElement);

                    elementBlock.MergeAttributes(formElement.ValidationAttributes);
                    elementBlock.MergeAttributes(formElement.AppendedHtmlAttributes);

                    sb.Append(elementBlock);
                }
                sb.Append(this.layoutBuilder.EndControlContainer(formElement, formBuilderParameters));
            }
            sb.Append(this.layoutBuilder.EndElementContainer(formElement, formBuilderParameters));

            return sb.ToString();
        }
 private TagBuilder RenderFloatingPointNumber(FormElement formElement)
 {
     var value = formElement.FieldValue;
     var tagbuilder = new TagBuilder("input");
     tagbuilder.MergeAttribute(HtmlAtrributes.Value, Convert.ToString(value));
     tagbuilder.MergeAttribute(HtmlAtrributes.Type, "text");
     tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
     tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
     return tagbuilder; 
 }
        protected void RenderElement(NavHtmlTextWritter writer, TModel model, FormElement formElement)
        {
            PropertyInfo property = formElement.PropertyInfo;

            bool isRequired = false;

            var requiredAttribute =
                property.GetCustomAttributes(typeof(RequiredAttribute), false).FirstOrDefault() as RequiredAttribute;

            if (requiredAttribute != null)
            {
                isRequired = true;
            }

            var value = property.GetValue(model, null);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "form-control");

            switch (formElement.ControlSpecs.Control)
            {
            case ControlType.TextBox:
                formControlGenerator.RenderTextBox(writer, formElement, value, isRequired);
                break;

            case ControlType.Hidden:
                formControlGenerator.RenderHidden(writer, formElement, value, isRequired);
                break;

            case ControlType.TextArea:
                formControlGenerator.RenderTextArea(writer, formElement, value, isRequired);
                break;

            case ControlType.Password:
                formControlGenerator.RenderPassword(writer, formElement, value, isRequired);
                break;

            case ControlType.DateTime:
                formControlGenerator.RenderDateTime(writer, formElement, value, isRequired);
                break;

            case ControlType.FloatingPointNumber:
                formControlGenerator.RenderFloatingPointNumber(writer, formElement, value, isRequired);
                break;

            case ControlType.WholeNumber:
                formControlGenerator.RenderWholeNumber(writer, formElement, value, isRequired);
                break;

            case ControlType.Time:
                formControlGenerator.RenderDateTime(writer, formElement, value, isRequired);
                break;

            case ControlType.CheckBox:
                formControlGenerator.RenderBoolean(writer, formElement, value);
                break;

            case ControlType.Enum:
                formControlGenerator.RenderEnum(writer, formElement, value, isRequired);
                break;

            case ControlType.DropDownList:
                var collectionObject = properties[formElement.CollectionInfo.ListSourceMember]
                                       .GetValue(model, null) as IEnumerable <SelectListItem>;
                formControlGenerator.RenderDropDownList(writer, formElement, value, isRequired, collectionObject);
                break;

            case ControlType.ListBox:
                break;

            case ControlType.StaticText:
                formControlGenerator.RenderStaticText(writer, formElement, value, isRequired);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 public void EndControlContainer(HtmlTextWriter textWriter, FormElement formElement, IFormBuilderParameters formBuilderParameters)
 {
     textWriter.RenderEndTag(); // div (ElementType-Group)
 }
Beispiel #17
0
        public string BeginControlContainer(FormElement formElement, IFormBuilderParameters formBuilderParameters)
        {
            var sb = new StringBuilder();
            var stringWriter = new StringWriter(sb);
            var textWriter = new HtmlTextWriter(stringWriter);
 
            var bootStrapBuilderParameters = (BuilderParameters) formBuilderParameters;
            var formType = bootStrapBuilderParameters.FormType;
            ControlContainer.Begin(textWriter, formType);

            return sb.ToString();
        }
        private FormElement FormElement(TModel model, PropertyInfo propertyInfo, HtmlHelper <TModel> htmlHelper)
        {
            var formElement = new FormElement
            {
                PropertyInfo = propertyInfo,
                ControlSpecs =
                    propertyInfo.GetCustomAttributes(typeof(EditControl), false)
                    .Cast <EditControl>()
                    .FirstOrDefault(),
                CollectionInfo = propertyInfo.GetCustomAttributes(typeof(CollectionInfo), false)
                                 .Cast <CollectionInfo>()
                                 .FirstOrDefault(),
                FieldValue = FieldValue(model, propertyInfo)
            };

            if (formElement.ControlSpecs == null)
            {
                return(null);
            }

            var displayAttribute = formElement.PropertyInfo.GetCustomAttributes(typeof(DisplayAttribute), false).Cast <DisplayAttribute>().FirstOrDefault();

            formElement.ControlSpecs.ControlName = formElement.ControlSpecs.ControlName ?? formElement.PropertyInfo.Name;
            formElement.ControlSpecs.ClientId    = formElement.ControlSpecs.ClientId ?? formElement.PropertyInfo.Name;
            formElement.MappedDataType           = formElement.PropertyInfo.PropertyType;

            var appendedHtmlAttributes = formElement.PropertyInfo.GetCustomAttributes(typeof(AppendedHtmlAttribute), false).Cast <AppendedHtmlAttribute>().ToList();

            if (appendedHtmlAttributes.Count >= 0)
            {
                formElement.AppendedHtmlAttributes = new Dictionary <string, object>();
                foreach (var attribute in appendedHtmlAttributes)
                {
                    formElement.AppendedHtmlAttributes.Add(attribute.Name, attribute.Value);
                }
            }

            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(formElement.ControlSpecs.ControlName, out modelState) && modelState.Errors.Count > 0)
            {
                formElement.HasErrors = true;
            }

            formElement.ValidationAttributes = this.GetValidationAttributes(htmlHelper, formElement.PropertyInfo.Name);

            if (displayAttribute != null)
            {
                formElement.ControlSpecs.GroupName = displayAttribute.GetGroupName();
                formElement.ControlSpecs.Name      = displayAttribute.GetName();
                formElement.ControlSpecs.Prompt    = displayAttribute.GetPrompt();
                formElement.ControlSpecs.Order     = displayAttribute.GetOrder();
                formElement.ControlSpecs.ShortName = displayAttribute.GetShortName();

                if (string.IsNullOrEmpty(formElement.ControlSpecs.ControlName))
                {
                    formElement.ControlSpecs.ControlName = formElement.PropertyInfo.Name;
                }

                if (string.IsNullOrEmpty(formElement.ControlSpecs.ClientId))
                {
                    formElement.ControlSpecs.ClientId = formElement.PropertyInfo.Name;
                }
            }

            if (formElement.CollectionInfo != null)
            {
                formElement.CollectionInfo.CollectionObject =
                    properties[formElement.CollectionInfo.ListSourceMember]
                    .GetValue(model, null) as IEnumerable <SelectListItem>;
            }

            return(formElement);
        }
        private TagBuilder RenderDateTime(FormElement formElement)
        {
            var value = formElement.FieldValue;
            var tagbuilder = new TagBuilder("input");
            tagbuilder.MergeAttribute(HtmlAtrributes.Value, Convert.ToString(value));
            tagbuilder.MergeAttribute(HtmlAtrributes.Type, "text");
            tagbuilder.MergeAttribute(HtmlAtrributes.Class, "datepicker");
            tagbuilder.MergeAttribute(HtmlAtrributes.Cols, "14");
            tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
            tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);

            return tagbuilder;
        }
        private TagBuilder RenderListBox(FormElement formElement)
        {
            var value = formElement.FieldValue as IList<string>;

            var itemsList = formElement.CollectionInfo.CollectionObject;
            var tagbuilder = new TagBuilder("select");

            tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
            tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
            tagbuilder.MergeAttribute(HtmlAtrributes.Multiple, null);
            tagbuilder.MergeAttribute(HtmlAtrributes.Size, formElement.ControlSpecs.Size.ToString());

            if (!string.IsNullOrEmpty(formElement.CollectionInfo.SelectPromptLabel))
            {
                var option = new TagBuilder("option");

                option.MergeAttribute(HtmlAtrributes.Value, formElement.CollectionInfo.SelectPromptValue);
                option.SetInnerText(formElement.CollectionInfo.SelectPromptLabel);

                if (value == null ||
                                    (formElement.CollectionInfo.SelectPromptValue != null
                                    && formElement.CollectionInfo.SelectPromptValue.ToUpper() == value.ToString().ToUpper())
                   )
                {
                    option.MergeAttribute(HtmlAtrributes.Selected, null);
                }

                tagbuilder.InnerHtml += option.ToMvcHtmlString(TagRenderMode.Normal);
            }

            if (itemsList != null)
            {
                foreach (var selectListItem in itemsList)
                {
                    var option = new TagBuilder("option");

                    option.MergeAttribute(HtmlAtrributes.Value, selectListItem.Value);
                    option.SetInnerText(selectListItem.Text);

                    if (value != null &&  value.Contains(selectListItem.Value.ToUpper()))
                    {
                        option.MergeAttribute(HtmlAtrributes.Selected, null);
                    }

                    tagbuilder.InnerHtml += option.ToMvcHtmlString(TagRenderMode.Normal);
                }
            }

            return tagbuilder;
        }
        private TagBuilder RenderEnum(FormElement formElement)
        {
            var value = formElement.FieldValue;
            var tagbuilder = new TagBuilder("select");
            
            
            foreach (var enumValue in Enum.GetValues(formElement.MappedDataType))
            {
                var option = new TagBuilder("option");
                option.MergeAttribute(HtmlAtrributes.Value, enumValue.ToString());
                
                var enumValueText = EnumExtensions.GetEnumDescription((Enum) enumValue);
                option.SetInnerText(enumValueText);
                var selected = value != null && (int)enumValue == (int)Enum.Parse(formElement.MappedDataType, value.ToString());

                if (selected)
                {
                    option.MergeAttribute(HtmlAtrributes.Selected, null);
                }

                tagbuilder.InnerHtml += option.ToMvcHtmlString(TagRenderMode.Normal);
            }

            tagbuilder.MergeAttribute("id", formElement.ControlSpecs.ClientId);
            tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
            return tagbuilder;
        }
 private TagBuilder RenderPassword(FormElement formElement)
 {
     var value = formElement.FieldValue;
     var tagbuilder = new TagBuilder("input");
   
     tagbuilder.MergeAttribute(HtmlAtrributes.Type, "password");
   
     if (formElement.ControlSpecs.MaxLength != 0)
     {
         tagbuilder.MergeAttribute(HtmlAtrributes.Maxlength, formElement.ControlSpecs.MaxLength.ToString(CultureInfo.InvariantCulture));
     }
     tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
     tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
     return tagbuilder;
 }
 private TagBuilder RenderTextArea(FormElement formElement)
 {
     var value = formElement.FieldValue;
     var tagbuilder = new TagBuilder("textarea");
     
     tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
     tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
     tagbuilder.MergeAttribute(HtmlAtrributes.Cols, formElement.ControlSpecs.Cols.ToString(CultureInfo.InvariantCulture));
     tagbuilder.MergeAttribute(HtmlAtrributes.Rows, formElement.ControlSpecs.Rows.ToString(CultureInfo.InvariantCulture));
     tagbuilder.SetInnerText(Convert.ToString(value));
     return tagbuilder; 
     
 }
        private TagBuilder RenderTextBox(FormElement formElement)
        {
            var value = formElement.FieldValue;
            var tagbuilder = new TagBuilder("input");
            tagbuilder.MergeAttribute("value", Convert.ToString(value));

            if (formElement.ControlSpecs.Size != 0)
            {
                tagbuilder.MergeAttribute("size", formElement.ControlSpecs.Size.ToString());
            }

            tagbuilder.MergeAttribute(HtmlAtrributes.Type, "text");
            
            if (formElement.ControlSpecs.MaxLength != 0)
            {
                tagbuilder.MergeAttribute(HtmlAtrributes.Maxlength, formElement.ControlSpecs.MaxLength.ToString(CultureInfo.InvariantCulture));
            }

            tagbuilder.MergeAttribute(HtmlAtrributes.Name, formElement.ControlSpecs.ControlName);
            tagbuilder.MergeAttribute(HtmlAtrributes.Id, formElement.ControlSpecs.ClientId);
            return tagbuilder;
        }