예제 #1
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);
        }