/// <summary> /// A <select> tag with >option< 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()); }
/// <summary> /// A <select> tag with >option< 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()); }
/// <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()); }
/// <summary> /// Creates a <span> 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.<control>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()); }
/// <summary> /// Creates a <span> 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.<control>For() element. /// </summary> /// <param name="model">The model itself.</param> /// <param name="markupName">The markup name of the Html.<control>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()); }
/// <summary> /// A <textarea> 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()); }
/// <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()); }
/// <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()); }
/// <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 <ul> child element with >li< 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()); }
/// <summary> /// Create a <label> tag for a validation-enabled control created from Html.<control>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()); }