/// <summary> /// Creates a Bootstrap date time picker control /// </summary> /// <typeparam name="TModel">Type to be scanned</typeparam> /// <typeparam name="TValue">Property to be scanned</typeparam> /// <param name="html">HtmlHelper extension</param> /// <param name="expression">The property lamba expression</param> /// <param name="options">Date time picker options</param> /// <param name="htmlAttributes">Extra HTML attributes to be applied to the text box</param> /// <returns>A Bootstrap date time picker control</returns> public static IExtendedHtmlStringLegacy DateTimeTextBoxFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, PickerOptions options, object htmlAttributes = (IDictionary <string, object>) null) { MemberExpression exp = expression.Body as MemberExpression; PickerOptions pickerOptions = options.GetHashCode() == BootstrapSettings.DateTimePickerOptions.GetHashCode() ? options.DeepClone() : options; pickerOptions = pickerOptions.TryFontAwesomeIcons(); var model = ((DateTime?)ModelMetadata.FromLambdaExpression(expression, html.ViewData).Model); if (model.HasValue && model.Value > DateTime.MinValue) { pickerOptions.defaultDate = model; } string fieldId = WebExtrasUtil.GetFieldIdFromExpression(exp); string fieldName = WebExtrasUtil.GetFieldNameFromExpression(exp); // create the text box HtmlComponent input = new HtmlComponent(EHtmlTag.Input); var attribs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) .ToDictionary(k => k.Key, v => v.Value.ToString()); input.Attributes.Add(attribs); input.Attributes["type"] = "text"; input.Attributes["name"] = fieldName; if (input.Attributes.ContainsKey("class")) { input.Attributes["class"] += " form-control"; } else { input.Attributes["class"] = "form-control"; } // create icon HtmlComponent icon = new HtmlComponent(EHtmlTag.I); if (WebExtrasSettings.FontAwesomeVersion == EFontAwesomeVersion.V4) { icon.CssClasses.Add("fa fa-calendar"); } else if (WebExtrasSettings.FontAwesomeVersion == EFontAwesomeVersion.V3) { icon.CssClasses.Add("icon-calendar"); } else { icon.CssClasses.Add("glyphicon glyphicon-calendar"); } // create addon HtmlComponent addOn = new HtmlComponent(EHtmlTag.Span); addOn.CssClasses.Add("input-group-addon"); addOn.AppendTags.Add(icon); // create JSON dictionary of the picker options string op = pickerOptions.ToJson(new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); HtmlComponent script = new HtmlComponent(EHtmlTag.Script); script.Attributes["type"] = "text/javascript"; script.InnerHtml = "$(function(){ $('#" + fieldId + "').datetimepicker(" + op + "); });"; // create the final component IHtmlComponent control; if (pickerOptions.useAddonField.Equals(false)) { NullWrapperComponent wrapper = new NullWrapperComponent(); input.Attributes["id"] = fieldId; wrapper.Components.Add(input); wrapper.Components.Add(script); control = wrapper; } else { control = new HtmlComponent(EHtmlTag.Div); control.Attributes["id"] = fieldId; control.Attributes["class"] = "input-group date"; control.AppendTags.Add(input); control.AppendTags.Add(addOn); control.AppendTags.Add(script); } return(new HtmlElement(control)); }
/// <summary> /// Creates a Bootstrap date time picker control /// </summary> /// <typeparam name="TModel">Type to be scanned</typeparam> /// <typeparam name="TValue">Property to be scanned</typeparam> /// <param name="html">HtmlHelper extension</param> /// <param name="expression">The property lamba expression</param> /// <param name="options">Date time picker options</param> /// <param name="htmlAttributes">Extra HTML attributes to be applied to the text box</param> /// <returns>A Bootstrap date time picker control</returns> public static MvcHtmlString DateTimeTextBoxFor <TModel, TValue>(this HtmlHelper <TModel> html, Expression <Func <TModel, TValue> > expression, PickerOptions options, object htmlAttributes = (IDictionary <string, object>) null) { MemberExpression exp = expression.Body as MemberExpression; PickerOptions pickerOptions = options.GetHashCode() == BootstrapSettings.DateTimePickerOptions.GetHashCode() ? options.DeepClone() : options; pickerOptions = pickerOptions.UpdateOptionsBasedOnView(); string fieldId = WebExtrasUtil.GetFieldIdFromExpression(exp); string fieldName = WebExtrasUtil.GetFieldNameFromExpression(exp); string datetimeformat = ConvertToCsDateFormat(pickerOptions.format); // create the text box TagBuilder input = new TagBuilder("input"); input.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); input.Attributes["type"] = "text"; input.Attributes["value"] = ((DateTime)ModelMetadata.FromLambdaExpression(expression, html.ViewData).Model).ToString(datetimeformat); input.Attributes["name"] = fieldName; if (input.Attributes.ContainsKey("class")) { input.Attributes["class"] += " form-control"; } else { input.Attributes["class"] = "form-control"; } // create addon TagBuilder addOn = new TagBuilder("span"); addOn.AddCssClass("add-on"); TagBuilder icons = new TagBuilder("i"); icons.AddCssClass("icon-calendar"); TagBuilder control = new TagBuilder("div"); control.Attributes["id"] = fieldId; control.Attributes["class"] = "input-append date form_datetime"; addOn.InnerHtml = icons.ToString(TagRenderMode.Normal); control.InnerHtml = input.ToString(TagRenderMode.SelfClosing) + addOn.ToString(TagRenderMode.Normal); // create JSON dictionary of the picker options string op = pickerOptions.ToJson(); TagBuilder script = new TagBuilder("script"); script.Attributes["type"] = "text/javascript"; script.InnerHtml = "$(function(){ $('#" + fieldId + "').datetimepicker(" + op + "); });"; return(MvcHtmlString.Create(control.ToString(TagRenderMode.Normal) + script.ToString(TagRenderMode.Normal))); }