Exemplo n.º 1
0
        public static IHtmlString BsLink(this HtmlHelper htmlHelper, object innerText, string buttonClass, string iconClass, string linkHref, object htmlAttributes = null, AjaxOptions ajaxOptions = null)
        {
            var mainAttrs = new Dictionary <string, object>();

            if (!String.IsNullOrWhiteSpace(linkHref))
            {
                mainAttrs.Add("href", linkHref);
            }
            if (!String.IsNullOrWhiteSpace(buttonClass))
            {
                mainAttrs.Add("class", buttonClass);
            }

            var link = new FluentTagBuilder("a")
                       .AddAttributes(mainAttrs)
                       .AddAttributes(htmlAttributes)
                       .AddContent(htmlHelper.Icon(iconClass, innerText));

            if (ajaxOptions != null)
            {
                link.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
            }

            return(link);
        }
Exemplo n.º 2
0
        protected virtual string BeginForm <TModel>(HtmlHelper htmlHelper, RoboUIFormResult <TModel> roboForm, string formId) where TModel : class
        {
            if (roboForm.Layout == RoboUIFormLayout.Wizard)
            {
                roboForm.FormActionUrl = null;
            }

            string formActionUrl = string.IsNullOrEmpty(roboForm.FormActionUrl)
                ? htmlHelper.ViewContext.HttpContext.Request.RawUrl
                : roboForm.FormActionUrl;

            var form = new FluentTagBuilder("form", TagRenderMode.StartTag)
                       .MergeAttribute("action", formActionUrl)
                       .MergeAttribute("id", formId)
                       .MergeAttribute("method", roboForm.FormMethod.ToString().ToLowerInvariant());

            if (roboForm.Layout == RoboUIFormLayout.Tab)
            {
                form = form.MergeAttribute("data-ajax-begin", formId.Replace("-", "_") + "_ValidateTabs");
            }

            if (!roboForm.AjaxEnabled)
            {
                form = form.MergeAttribute("enctype", "multipart/form-data");
            }
            else
            {
                form = form.MergeAttribute("data-ajax", "true");
            }

            form = form.MergeAttribute("method", roboForm.FormMethod.ToString().ToLowerInvariant());
            form = form.MergeAttributes(roboForm.HtmlAttributes);

            return(form.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create combo date selector for date time.
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// When expression is null.
        /// </exception>
        /// <typeparam name="TModel">Type of model.</typeparam>
        /// <param name="htmlHelper">HtmlHelper.</param>
        /// <param name="expression">Property selector expression.</param>
        /// <param name="yearRange">Range of years.</param>
        /// <param name="htmlAttributes">Html attributes to add to combo date.</param>
        /// <returns>Combo date selector.</returns>
        public static MvcHtmlString ComboDateFor <TModel>(
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, DateTime> > expression,
            IEnumerable <int> yearRange,
            object htmlAttributes)

        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            string comboDateCss      = "combo-date";
            string dayContainerCss   = "day-container";
            string monthContainerCss = "month-container";
            string yearContainerCss  = "year-container";
            string errorCss          = "combo-date__error";

            string dayText   = "Gün";
            string monthText = "Ay";
            string yearText  = "İl";

            // Initialize yearRange if has not been provided
            if (yearRange == null)
            {
                yearRange = Enumerable.Range(1900, 200);
            }

            // Get model metadata
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(
                expression,
                htmlHelper.ViewData);

            string modelName = ExpressionHelper.GetExpressionText(expression);

            // Append HtmlFieldPrefix if there is any
            string fieldPrefix = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix;

            if (!string.IsNullOrEmpty(fieldPrefix))
            {
                modelName = string.Format("{0}.{1}", fieldPrefix, modelName);
            }

            // Convert html attributes
            RouteValueDictionary attributes = HtmlHelper
                                              .AnonymousObjectToHtmlAttributes(htmlAttributes);

            // Initialize container div
            FluentTagBuilder comboDate = new FluentTagBuilder("div")
                                         .MergeAttributes(attributes)
                                         .AddCssClass(comboDateCss);

            // Initialize hidden text box for client side validation
            FluentTagBuilder input = new FluentTagBuilder("input")
                                     .MergeAttribute("name", modelName)
                                     .MergeAttribute("id", modelName)
                                     .MergeAttribute("type", "date")
                                     .MergeAttribute("hidden", "hidden")
                                     .MergeAttribute("readonly", "readonly");

            if (metadata.Model != null)
            {
                DateTime value = Convert.ToDateTime(metadata.Model);
                input.MergeAttribute("value", value.ToString("yyyy-MM-dd"));
            }

            //// Get validation attributes
            IDictionary <string, object> validationAttributes =
                htmlHelper.GetUnobtrusiveValidationAttributes(modelName, metadata);

            // Merge validation attributes
            input.MergeAttributes(validationAttributes);

            //contentBuilder.AppendLine(input.ToString());
            comboDate.AppendChild(input);

            // Declare date property selector
            Expression <Func <TModel, Int32> > datePropertySelector;

            // Select day property of date
            MemberExpression dayExpression = Expression.Property(expression.Body, "Day");

            datePropertySelector = Expression.Lambda <Func <TModel, Int32> >(
                dayExpression,
                expression.Parameters);

            // Create drop down button for day
            MvcHtmlString daySelector = htmlHelper
                                        .DropDownButtonFor <TModel, int>(
                datePropertySelector,
                new SelectList(Enumerable
                               .Range(1, 31)
                               .Select(m => new SelectListItem
            {
                Text  = m.ToString("00"),
                Value = m.ToString()
            })),
                dayText);

            // Setup day container
            FluentTagBuilder dayContainer = new FluentTagBuilder("div")
                                            .AddCssClass(dayContainerCss)
                                            .AppendChild(daySelector);

            //contentBuilder.AppendLine(dayContainer.ToString());

            comboDate.AppendChild(dayContainer);

            // Select month property of date
            MemberExpression monthExpression = Expression.Property(expression.Body, "Month");

            datePropertySelector = Expression.Lambda <Func <TModel, Int32> >(
                monthExpression,
                expression.Parameters);

            // Create drop down button for month
            MvcHtmlString monthSelector = htmlHelper
                                          .DropDownButtonFor <TModel, int>(
                datePropertySelector,
                new SelectList(Enumerable.Range(1, 12)
                               .Select(r => new SelectListItem
            {
                Value = r.ToString(),
                Text  = DateTimeFormatInfo.CurrentInfo.GetMonthName(r)
            })),
                monthText);

            // Setup month container
            FluentTagBuilder monthContainer = new FluentTagBuilder("div")
                                              .AddCssClass(monthContainerCss)
                                              .AppendChild(monthSelector);

            //contentBuilder.AppendLine(monthContainer.ToString());

            comboDate.AppendChild(monthContainer);

            // Select year property of date
            MemberExpression yearExpression = Expression.Property(expression.Body, "Year");

            datePropertySelector = Expression.Lambda <Func <TModel, Int32> >(
                yearExpression,
                expression.Parameters);

            // Create drop down button for month
            MvcHtmlString yearSelector = htmlHelper
                                         .DropDownButtonFor <TModel, int>(
                datePropertySelector,
                new SelectList(yearRange
                               .Select(r => new SelectListItem
            {
                Text  = r.ToString(),
                Value = r.ToString()
            })),
                yearText);

            // Setup year container
            FluentTagBuilder yearContainer = new FluentTagBuilder("div")
                                             .AddCssClass(yearContainerCss)
                                             .AppendChild(yearSelector);

            comboDate.AppendChild(yearContainer);

            // Set up error span
            MvcHtmlString validationMessage = htmlHelper
                                              .ValidationMessageFor(expression);

            FluentTagBuilder errorSpan = new FluentTagBuilder("span")
                                         .AddCssClass(errorCss)
                                         .AppendChild(validationMessage);

            comboDate.AppendChild(errorSpan);

            return(new MvcHtmlString(comboDate.Render()));
        }