Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 3
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());
        }
Ejemplo n.º 4
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());
        }
Ejemplo n.º 5
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);
        }