예제 #1
0
        /// <summary>
        /// A &lt;select&gt; tag with &gt;option&lt; elements with validation enabled.
        /// </summary>
        /// <param name="markupname">The name/id of the select tag.</param>
        /// <param name="name">The name of the property from the model.</param>
        /// <param name="model">The model itself.</param>
        /// <param name="selectList">The items in the select tag.</param>
        /// <param name="optionlabel">The text for the default empty item. This parameter can be null.</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns>Returns a select tag with option children.</returns>
        public string DropDownListFor(string markupname, string name, object model, IEnumerable <SelectListItem> selectList, string optionlabel, object htmlProperties)
        {
            HtmlTag sel = new HtmlTag("select", new { id = markupname, name = markupname });

            HtmlTag optx = new HtmlTag("option", new { value = "" })
            {
                InnerText = optionlabel ?? ""
            };

            sel.Children.Add(optx);

            foreach (SelectListItem si in selectList)
            {
                HtmlTag opt = new HtmlTag("option", new { value = si.Value ?? "" })
                {
                    InnerText = si.Text ?? ""
                };
                if (si.Selected)
                {
                    opt.Attr("selected", "selected");
                }
                sel.Children.Add(opt);
            }
            if (htmlProperties != null)
            {
                sel.MergeObjectProperties(htmlProperties);
            }

            sel = PreProcess(sel, _MetaData, TagTypes.Select, markupname, name, model);

            return(sel.Render());
        }
예제 #2
0
        /// <summary>
        /// A &lt;select&gt; tag with &gt;option&lt; elements without validation enabled.
        /// </summary>
        /// <param name="name">The name/id of the select tag.</param>
        /// <param name="selectList">The items in the select tag.</param>
        /// <param name="optionlabel">The text for the default empty item. This parameter can be null.</param>
        /// <returns>Returns a select tag with option children.</returns>
        public string DropDownList(string name, IEnumerable <SelectListItem> selectList, string optionlabel)
        {
            HtmlTag sel = new HtmlTag("select", new { id = name, name = name });

            HtmlTag optx = new HtmlTag("option", new { value = "" })
            {
                InnerText = optionlabel ?? ""
            };

            sel.Children.Add(optx);

            foreach (SelectListItem si in selectList)
            {
                HtmlTag opt = new HtmlTag("option", new { value = si.Value ?? "" })
                {
                    InnerText = si.Text ?? ""
                };
                if (si.Selected)
                {
                    opt.Attr("selected", "selected");
                }
                sel.Children.Add(opt);
            }

            sel = PreProcess(sel, _MetaData, TagTypes.Select, name, "", null);

            return(sel.Render());
        }
예제 #3
0
        /// <summary>
        /// A textbox with validation enabled.
        /// </summary>
        /// <param name="markupname">The name/id of the textbox in markup.</param>
        /// <param name="name">The name of the property from the model.</param>
        /// <param name="model">The model itself.</param>
        /// <param name="value">The initial value of the textbox, usually from the model.</param>
        /// <param name="htmlAttributes">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns>Returns an input control.</returns>
        public string TextBoxFor(string markupname, string name, object model, object value, object htmlAttributes)
        {
            HtmlTag tb = new HtmlTag("input", new { type = "text", name = markupname, value = (value ?? "").ToString(), id = markupname }, true);

            tb.MergeObjectProperties(htmlAttributes);

            tb = PreProcess(tb, _MetaData, TagTypes.InputBox, markupname, name, model);

            return(tb.Render());
        }
예제 #4
0
        /// <summary>
        /// Creates a &lt;span&gt; tag with appropriate validation information used by client side AND server side code.<br/>
        /// WFUtilities.FieldValidationErrorClass is applied if validation fails on a postback. This is used with an Html.&ltcontrol&gt;For() element.<br/>
        /// The property whose validation state is checked is derived from a strongly-typed lambda.
        /// </summary>
        /// <param name="ErrorMessage">(Optional) Override any ErrorMessage provided by resources/xml/validators with this property.</param>
        /// <param name="expression">An expression that identifies the property whose value will be rendered.<br/>
        /// ie: m => m.FirstName will render the 'FirstName' property.</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns></returns>
        public string ValidationMessageFor <TProperty>(Expression <Func <TModel, TProperty> > expression, string ErrorMessage, object htmlProperties)
        {
            WFModelMetaProperty metaprop = null;
            ModelMetaData       mmd      = ModelMetaData.FromLambdaExpression(expression, _Model);
            HtmlTag             span     = new HtmlTag("span", new { id = mmd.PropertyName + "_validationMessage", name = mmd.PropertyName + "_validationMessage" });
            string lcName = mmd.PropertyName.ToLower();

            for (int i = 0; i < _MetaData.Properties.Count; i++)
            {
                if (_MetaData.Properties[i].MarkupName.ToLower() == lcName)
                {
                    metaprop = _MetaData.Properties[i];
                    break;
                }
            }
            if (metaprop != null)
            {
                if (metaprop.HasError)
                {
                    span.MergeObjectProperties(htmlProperties);
                    span.AddClass(WFUtilities.FieldValidationErrorClass);
                    if (String.IsNullOrEmpty(ErrorMessage))
                    {
                        span.InnerText = metaprop.Errors.FirstOrDefault() ?? "";
                    }
                    else
                    {
                        span.InnerText = ErrorMessage;
                    }
                    return(span.Render());
                }
            }

            span.MergeObjectProperties(htmlProperties);
            span.AddClass(WFUtilities.FieldValidationValidClass);

            span = PreProcess(span, _MetaData, TagTypes.ValidationMessage,
                              metaprop != null ? metaprop.MarkupName : mmd.PropertyName,
                              metaprop != null ? metaprop.PropertyName : mmd.PropertyName,
                              _Model);

            return(span.Render());
        }
예제 #5
0
        /// <summary>
        /// Creates a &lt;span&gt; tag with appropriate validation information used by client side AND server side code.
        /// WFUtilities.FieldValidationErrorClass is applied if validation fails on a postback. This is used with an Html.&ltcontrol&gt;For() element.
        /// </summary>
        /// <param name="model">The model itself.</param>
        /// <param name="markupName">The markup name of the Html.&lt;control&gt;For() element.</param>
        /// <param name="propertyName">Must be the property name on the model object.</param>
        /// <param name="ErrorMessage">Override the error message (use with care)</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns>Returns a span with WFUtilities.FieldValidationErrorClass or WFUtilities.FieldValidationValidClass as the class.</returns>
        public string ValidationMessageFor(object model, string markupName, string propertyName, string ErrorMessage, object htmlProperties)
        {
            WFModelMetaProperty metaprop = null;
            HtmlTag             span     = new HtmlTag("span", new { id = markupName + "_validationMessage", name = markupName + "_validationMessage" });

            string lcName = propertyName.ToLower();

            for (int i = 0; i < _MetaData.Properties.Count; i++)
            {
                //if (_MetaData.Properties[i].ModelObject == model && _MetaData.Properties[i].PropertyName.ToLower() == lcName)
                if (_MetaData.Properties[i].MarkupName.ToLower() == lcName)
                {
                    metaprop = _MetaData.Properties[i];
                    break;
                }
            }
            if (metaprop != null)
            {
                if (metaprop.HasError)
                {
                    span.MergeObjectProperties(htmlProperties);
                    span.AddClass(WFUtilities.FieldValidationErrorClass);

                    if (String.IsNullOrEmpty(ErrorMessage))
                    {
                        span.InnerText = metaprop.Errors.FirstOrDefault() ?? "";
                    }
                    else
                    {
                        span.InnerText = ErrorMessage;
                    }
                    return(span.Render());
                }
            }

            span.MergeObjectProperties(htmlProperties);
            span.AddClass(WFUtilities.FieldValidationValidClass);

            span = PreProcess(span, _MetaData, TagTypes.ValidationMessage, markupName, propertyName, model);

            return(span.Render());
        }
예제 #6
0
        /// <summary>
        /// A &lt;textarea&gt; element without validation enabled.
        /// </summary>
        /// <param name="name">The name/id of the textarea element.</param>
        /// <param name="value">The innertext of the textarea element.</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns>Returns a textarea element.</returns>
        public string TextArea(string name, object value, object htmlProperties)
        {
            HtmlTag txa = new HtmlTag("textarea", new { cols = "20", rows = "2", name = name, id = name })
            {
                InnerText = (value ?? "").ToString()
            };

            txa.MergeObjectProperties(htmlProperties);

            txa = PreProcess(txa, _MetaData, TagTypes.TextArea, name, "", null);

            return(txa.Render());
        }
예제 #7
0
        /// <summary>
        /// A radio button with validation enabled.
        /// </summary>
        /// <param name="markupname">The name of the radio button. ID is not set automatically.</param>
        /// <param name="name">The name of the property from the model.</param>
        /// <param name="model">The model itself.</param>
        /// <param name="value">The value property of the radio button.</param>
        /// <param name="isChecked">Whether checked="checked" should be used.</param>
        /// <param name="htmlAttributes">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns>Returns an input control.</returns>
        public string RadioButtonFor(string markupname, string name, object model, object value, bool isChecked, object htmlAttributes)
        {
            HtmlTag rb = new HtmlTag("input", new { type = "radio", name = markupname, value = (value ?? "").ToString() });

            if (isChecked)
            {
                rb.Attr("checked", "checked");
            }

            rb.MergeObjectProperties(htmlAttributes);

            PreProcess(rb, _MetaData, TagTypes.RadioButton, markupname, name, model);

            return(rb.Render());
        }
예제 #8
0
        /// <summary>
        /// A checkbox control with validation enabled.
        /// </summary>
        /// <param name="markupname">The name/id of the checkbox.</param>
        /// <param name="name">The name of the property from the model.</param>
        /// <param name="model">The model itself.</param>
        /// <param name="isChecked">Whether checked="checked" should be used.</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns></returns>
        public string CheckboxFor(string markupname, string name, object model, bool?isChecked, object htmlProperties)
        {
            HtmlTag cb = new HtmlTag("input", new { type = "checkbox", id = markupname, name = markupname });

            if (isChecked.HasValue && isChecked.Value)
            {
                cb.Attr("checked", "checked");
            }
            if (htmlProperties != null)
            {
                cb.MergeObjectProperties(htmlProperties);
            }

            cb = PreProcess(cb, _MetaData, TagTypes.Checkbox, markupname, name, model);

            return(cb.Render());
        }
예제 #9
0
        /// <summary>
        /// Renders a div tag with id 'validationSummary'. The class is WFUtilities.ValidationSummaryValidClass or WFUtilities.ValidationSummaryErrorsClass depending on server validation.<br/>
        /// Errors are rendered in a &lt;ul&gt; child element with &gt;li&lt; children.
        /// </summary>
        /// <param name="model">The model object being validated.</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <returns>Returns a div tag for validation summary.</returns>
        public string ValidationSummary(object model, object htmlProperties)
        {
            List <string> errors = new List <string>();

            foreach (WFModelMetaProperty prop in _MetaData.Properties)
            {
                if (prop.HasError)
                {
                    foreach (string err in prop.Errors)
                    {
                        errors.Add(err);
                    }
                }
            }
            HtmlTag div = new HtmlTag("div", new { id = "validationSummary" });

            if (errors.Count > 0)
            {
                div.AddClass(WFUtilities.ValidationSummaryErrorsClass);
                HtmlTag ul = new HtmlTag("ul");
                foreach (string err in errors)
                {
                    HtmlTag li = new HtmlTag("li");
                    li.InnerText = err;
                    ul.Children.Add(li);
                }
                div.Children.Add(ul);
            }
            else
            {
                div.AddClass(WFUtilities.ValidationSummaryValidClass);
                HtmlTag ul = new HtmlTag("ul");
                HtmlTag li = new HtmlTag("li", new { style = "display: none;" });
                li = PreProcess(li, _MetaData, TagTypes.ValidationItem, "", "", model);
                ul.Children.Add(li);
                div.Children.Add(ul);
            }
            //<div class="validation-summary-valid" id="validationSummary"><ul><li style="display:none"></li></ul></div>

            div.MergeObjectProperties(htmlProperties);

            div = PreProcess(div, _MetaData, TagTypes.ValidationSummary, "", "", model);

            return(div.Render());
        }
예제 #10
0
        /// <summary>
        /// Create a &lt;label&gt; tag for a validation-enabled control created from Html.&lt;control&gt;For()<br/>
        /// The inner text is determined by the [DisplayName] attribute on the model property or an overload.
        /// </summary>
        /// <param name="markupName">Set as the 'For' property on the label element.</param>
        /// <param name="name">Must be the property name on the Model object</param>
        /// <param name="model">The model itself.</param>
        /// <param name="htmlProperties">An anonymous object whose properties are applied to the element.<br/>
        /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
        /// <param name="displayName">Override the display name (inner text) of the label.</param>
        /// <returns>Returns a label control.</returns>
        public string LabelFor(string markupName, string name, object model, object htmlProperties, string displayName)
        {
            string       dispName = "";
            PropertyInfo pi       = model.GetType().GetProperties().FirstOrDefault(p => p.Name == name);

            if (pi == null)
            {
                throw new Exception("[" + name + "] public property not found on object [" + model.GetType().Name + "]");
            }
            if (String.IsNullOrEmpty(displayName))
            {
                DisplayNameAttribute datt = pi.GetCustomAttributes(false).OfType <DisplayNameAttribute>().FirstOrDefault();
                if (datt != null)
                {
                    dispName = datt.DisplayName ?? pi.Name;
                }
                else
                {
                    dispName = pi.Name;
                }
            }
            else
            {
                dispName = displayName;
            }
            HtmlTag lbl = new HtmlTag("label", new { For = markupName })
            {
                InnerText = dispName
            };

            lbl.MergeObjectProperties(htmlProperties);

            lbl = PreProcess(lbl, _MetaData, TagTypes.Label, markupName, name, model);

            return(lbl.Render());
        }