コード例 #1
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());
        }
コード例 #2
0
        /// <summary>
        /// Returns the raw text of any generated server-side validation error for this property.<br/>
        /// This is useful if you want to override the default functionality that generates a &lt;span&gt; tag for validation error messages.
        /// </summary>
        /// <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>
        /// <returns></returns>
        public string TextValidationMessageFor <TProperty>(Expression <Func <TModel, TProperty> > expression)
        {
            WFModelMetaProperty metaprop = null;
            ModelMetaData       mmd      = ModelMetaData.FromLambdaExpression(expression, _Model);
            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)
                {
                    return(metaprop.Errors.FirstOrDefault() ?? "");
                }
            }
            return("");
        }
コード例 #3
0
        private static HtmlTag GetTagFromExpression <TProperty>(TagTypes tagType,
                                                                Expression <Func <TModel, TProperty> > expression,
                                                                TModel model,
                                                                WFModelMetaData metadata,
                                                                object htmlProperties,
                                                                HtmlHelper <TModel> htmlHelper,
                                                                IEnumerable <SelectListItem> selectList,
                                                                string optionLabel, bool useLabel, bool isChecked)
        {
            ModelMetaData mmd = ModelMetaData.FromLambdaExpression(expression, model);

            string reflectName = mmd.PropertyName;
            string markupName  = mmd.PropertyName;

            HtmlTag tag = null;

            if (tagType == TagTypes.InputBox)
            {
                tag = new HtmlTag("input", true);
                tag.Attr("name", mmd.PropertyName);
                tag.Attr("id", mmd.PropertyName);
                tag.Attr("type", "text");
                tag.Attr("value", GetHTMLValue(mmd.ModelAccessor()));
            }
            else if (tagType == TagTypes.Checkbox)
            {
                tag = new HtmlTag("input", true);
                tag.Attr("name", mmd.PropertyName);
                tag.Attr("id", mmd.PropertyName);
                tag.Attr("type", "checkbox");
                if (GetHTMLValueAsBoolean(mmd.ModelAccessor()))
                {
                    tag.Attr("checked", "checked");
                }
            }
            else if (tagType == TagTypes.Hidden)
            {
                tag = new HtmlTag("input", true);
                tag.Attr("type", "hidden");
                tag.Attr("value", GetHTMLValue(mmd.ModelAccessor()));
                tag.Attr("name", mmd.PropertyName);
                tag.Attr("id", mmd.PropertyName);
            }
            else if (tagType == TagTypes.Label)
            {
                tag = new HtmlTag("label");
                tag.Attr("For", mmd.PropertyName);

                PropertyInfo         pi   = (PropertyInfo)((MemberExpression)expression.Body).Member;
                DisplayNameAttribute datt = pi.GetCustomAttributes(false).OfType <DisplayNameAttribute>().FirstOrDefault();
                string dispName           = "";
                if (datt != null)
                {
                    dispName = datt.DisplayName ?? pi.Name;
                }
                else
                {
                    dispName = pi.Name;
                }

                tag.InnerText = dispName;
            }
            else if (tagType == TagTypes.RadioButton)
            {
                tag = new HtmlTag("input", true);
                tag.Attr("name", mmd.PropertyName);
                tag.Attr("id", mmd.PropertyName);
                tag.Attr("type", "radio");
                tag.Attr("value", GetHTMLValue(mmd.ModelAccessor()));
                if (isChecked)
                {
                    tag.Attr("checked", "checked");
                }
            }
            else if (tagType == TagTypes.Select)
            {
                tag = new HtmlTag("select");
                tag.Attr("id", mmd.PropertyName);
                tag.Attr("name", mmd.PropertyName);

                if (useLabel)
                {
                    HtmlTag optx = new HtmlTag("option", new { value = "" })
                    {
                        InnerText = optionLabel ?? ""
                    };
                    tag.Children.Add(optx);
                }

                if (selectList != null)
                {
                    foreach (SelectListItem si in selectList)
                    {
                        HtmlTag opt = new HtmlTag("option", new { value = si.Value ?? "" })
                        {
                            InnerText = si.Text ?? ""
                        };
                        if (si.Selected)
                        {
                            opt.Attr("selected", "selected");
                        }
                        tag.Children.Add(opt);
                    }
                }
            }
            else if (tagType == TagTypes.TextArea)
            {
                tag = new HtmlTag("textarea");
                tag.Attr("cols", "20");
                tag.Attr("rows", "2");
                tag.Attr("name", mmd.PropertyName);
                tag.Attr("id", mmd.PropertyName);
                tag.InnerText = GetHTMLValue(mmd.ModelAccessor());
            }
            else if (tagType == TagTypes.Span)
            {
                tag = new HtmlTag("span");
                tag.Attr("id", mmd.PropertyName);
                tag.InnerText = GetHTMLValue(mmd.ModelAccessor());
            }

            //WFUtilities.CheckPropertyError(metadata, model, tag, mmd.PropertyName, mmd.PropertyName);
            tag.MergeObjectProperties(htmlProperties);

            if (((MemberExpression)expression.Body).Member is PropertyInfo)
            {
                tag = htmlHelper.PreProcess(tag, metadata, tagType, mmd.PropertyName, (PropertyInfo)((MemberExpression)expression.Body).Member, model);
            }
            else
            {
                throw new Exception("Invalid argument specified in lambda for Html.xFor() method [" + markupName + "] (must be a property). Check your markup.");
            }

            return(tag);
        }
コード例 #4
0
        /// <summary>
        /// Returns the raw ID of any generated server-side validation error for this property.<br/>
        /// This is useful if you want to override the default functionality that generates a &lt;span&gt; tag for validation error messages.
        /// </summary>
        /// <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>
        /// <returns></returns>
        public string TextValidationMessageIDFor <TProperty>(Expression <Func <TModel, TProperty> > expression)
        {
            ModelMetaData mmd = ModelMetaData.FromLambdaExpression(expression, _Model);

            return(mmd.PropertyName + "_validationMessage");
        }