Exemple #1
0
        public MvcHtmlString LabelFor <TProperty>(Expression <Func <TModel, TProperty> > expression, IDictionary <string, object> htmlAttributes)
        {
            int labelSize = BsHelper.GetLabelSize();

            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }

            if (htmlAttributes.ContainsKey("class"))
            {
                htmlAttributes["class"] += " control-label col-sm-" + labelSize.ToString();
            }
            else
            {
                htmlAttributes["class"] = "control-label col-sm-" + labelSize.ToString();
            }

            ViewDataDictionary <TModel> viewData = new ViewDataDictionary <TModel>(HtmlHelper.ViewData);

            ModelMetadata metadata      = ModelMetadata.FromLambdaExpression(expression, viewData);
            string        htmlFieldName = ExpressionHelper.GetExpressionText(expression);

            if (metadata.DisplayName == null)
            {
                metadata.DisplayName = BsHelper.SplitCamelCase(metadata.PropertyName ?? htmlFieldName.Split('.').Last());
            }

            return(HtmlHelper.LabelFor(expression, metadata.DisplayName, htmlAttributes));
        }
Exemple #2
0
        public DatePicker.DatePickerBuilder DatePickerFor <TProperty>(Expression <Func <TModel, TProperty> > expression, IDictionary <string, object> htmlAttributes)
        {
            ViewDataDictionary <TModel> viewData = new ViewDataDictionary <TModel>(HtmlHelper.ViewData);
            ModelMetadata metadata      = ModelMetadata.FromLambdaExpression(expression, viewData);
            string        htmlFieldName = ExpressionHelper.GetExpressionText(expression);

            string labelText = metadata.DisplayName;

            if (labelText == null)
            {
                labelText = BsHelper.SplitCamelCase(metadata.PropertyName ?? htmlFieldName.Split('.').Last());
            }

            // add a placeholder if it isn't already there
            if (!String.IsNullOrEmpty(labelText))
            {
                if (!htmlAttributes.ContainsKey("placeholder"))
                {
                    htmlAttributes["placeholder"] = "{placeholder}";
                }
            }

            // add form-control
            if (htmlAttributes.ContainsKey("class"))
            {
                htmlAttributes["class"] += " form-control date";
            }
            else
            {
                htmlAttributes["class"] = "form-control date";
            }


            DateTime?dtval = null;
            string   val   = HtmlHelper.ValueFor <TModel, TProperty>(expression).ToString();
            string   input = HtmlHelper.TextBoxFor(expression, htmlAttributes).ToString();

            if (val.Length > 0)
            {
                input = input.Replace(val, "{value}");
                DateTime temp;
                if (DateTime.TryParse(val, out temp))
                {
                    dtval = temp;
                }
            }


            DatePicker.DatePicker component = new DatePicker.DatePicker(htmlFieldName);
            component.DisplayName = labelText;

            DatePickerBuilder builder = new DatePickerBuilder(component, input, dtval);

            return(builder);
        }
Exemple #3
0
        public MvcHtmlString TextAreaFor <TProperty>(Expression <Func <TModel, TProperty> > expression, IDictionary <string, object> htmlAttributes)
        {
            // make sure that htmlAttributes is not null, or we won't be able to add to it
            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary <string, object>();
            }

            // get the field name for use with bootstrap
            ModelMetadata metadata      = ModelMetadata.FromLambdaExpression(expression, HtmlHelper.ViewData);
            string        htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            string        labelText     = metadata.DisplayName;

            if (labelText == null)
            {
                labelText = BsHelper.SplitCamelCase(metadata.PropertyName ?? htmlFieldName.Split('.').Last());
            }

            // add a placeholder if it isn't already there
            if (!String.IsNullOrEmpty(labelText))
            {
                if (!htmlAttributes.ContainsKey("placeholder"))
                {
                    htmlAttributes["placeholder"] = labelText;
                }
            }

            // add form-control
            if (htmlAttributes.ContainsKey("class"))
            {
                htmlAttributes["class"] += " form-control";
            }
            else
            {
                htmlAttributes["class"] = "form-control";
            }

            return(HtmlHelper.TextAreaFor(expression, htmlAttributes));
        }
Exemple #4
0
        public MvcHtmlString DisplayFor <TProperty>(Expression <Func <TModel, TProperty> > expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, HtmlHelper.ViewData);

            string htmlFieldName = ExpressionHelper.GetExpressionText(expression).Split('.').LastOrDefault();

            if (metadata.DisplayName == null)
            {
                metadata.DisplayName = BsHelper.SplitCamelCase(metadata.PropertyName ?? htmlFieldName.Split('.').Last());
            }

            MvcHtmlString expected = HtmlHelper.DisplayFor(expression);

            if (expression.ReturnType == typeof(Boolean) || expression.ReturnType == typeof(Boolean?))
            {
                if (expected.ToString().Contains("checked"))
                {
                    return(new MvcHtmlString("Yes"));
                }
                return(new MvcHtmlString("No"));
            }
            else if (expression.ReturnType == typeof(DateTime) || expression.ReturnType == typeof(DateTime?))
            {
                DateTime testDate;
                if (DateTime.TryParse(expected.ToString(), out testDate))
                {
                    // so we have a date, but we want it to look good
                    // if the time is midnight, we don't need to show the time
                    if (testDate.TimeOfDay.Ticks == 0)
                    {
                        return(new MvcHtmlString(testDate.ToString("MMMM d, yyyy")));
                    }
                    else
                    {
                        return(new MvcHtmlString(testDate.ToString("MMMM d, yyyy H:mm tt")));
                    }
                }
            }
            else if (expression.ReturnType == typeof(SelectList))
            {
                SelectList value = (SelectList)metadata.Model;
                var        items = value.AsEnumerable();

                if (items.Count() == 0)
                {
                    return(new MvcHtmlString(String.Format("<em class='undefined-display'>No {0} Defined</em>", metadata.DisplayName)));
                }

                string toReturn = "<ul>";
                foreach (var item in items)
                {
                    toReturn += "<li>" + item.Text + "</li>";
                }
                toReturn += "</ul>";

                return(new MvcHtmlString(toReturn));
            }

            if (!String.IsNullOrWhiteSpace(expected.ToString()))
            {
                return(expected);
            }

            return(new MvcHtmlString(String.Format("<em class='undefined-display'>No {0} Defined</em>", metadata.DisplayName)));
        }