protected string RenderPackedList() { PackedList <IEnumerable <TValue>, TValue> displayModel = new PackedList <IEnumerable <TValue>, TValue>(); displayModel.ImportFromModel(Value, new object[] { DualSelect_Separator }); StringBuilder sb = new StringBuilder(); sb.Append(BasicHtmlHelper.RenderDisplayInfo(CurrHtmlHelper, typeof(PackedList <IEnumerable <TValue>, TValue>), Prefix)); sb.AppendFormat("<span {2} style='display:none' id='{1}' is-item-control='true' pname='{0}' data-element-type = 'DualSelect' ></span>", _fullprefix, BasicHtmlHelper.IdFromName(_fullprefix), BasicHtmlHelper.GetAttributesString(AttributeExtensions)); sb.Append( CurrHtmlHelper.Hidden(BasicHtmlHelper.AddField(Prefix, "$.Separator"), DualSelect_Separator, new RouteValueDictionary { { "data-elementispart", "true" } })).ToString(); sb.Append(CurrHtmlHelper.Hidden( BasicHtmlHelper.AddField(Prefix, "$.PackedValue"), displayModel.PackedValue, new RouteValueDictionary { { "data-elementispart", "true" } })); return(sb.ToString()); }
internal HtmlHelper <T> GetHelper(HtmlHelper <M> htmlHelper, string partialPrefix, string prefix) { htmlHelper.ViewContext.Writer.Write( BasicHtmlHelper.RenderDisplayInfo(htmlHelper, this.GetType(), partialPrefix)); ViewDataDictionary <T> dataDictionary = new ViewDataDictionary <T>(FilterDescription); dataDictionary.TemplateInfo.HtmlFieldPrefix = BasicHtmlHelper.AddField(prefix, "$.FilterDescription"); BasicHtmlHelper.CopyRelevantErrors(dataDictionary.ModelState, htmlHelper.ViewData.ModelState, dataDictionary.TemplateInfo.HtmlFieldPrefix); return(new TemplateInvoker <T>().BuildHelper(htmlHelper, dataDictionary)); }
internal MvcHtmlString Render(HtmlHelper <M> htmlHelper, string partialPrefix, string prefix, object template) { StringBuilder sb = new StringBuilder(); sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper, this.GetType(), partialPrefix)); ViewDataDictionary <T> dataDictionary = new ViewDataDictionary <T>(FilterDescription); dataDictionary.TemplateInfo.HtmlFieldPrefix = BasicHtmlHelper.AddField(prefix, "$.FilterDescription"); BasicHtmlHelper.CopyRelevantErrors(dataDictionary.ModelState, htmlHelper.ViewData.ModelState, dataDictionary.TemplateInfo.HtmlFieldPrefix); sb.Append(new TemplateInvoker <T>(template).Invoke <M>(htmlHelper, dataDictionary)); return(MvcHtmlString.Create(sb.ToString())); }
protected string RenderBasicScripts(CultureInfo format) { if (baseScriptsRendered) { return(string.Empty); } baseScriptsRendered = true; StringBuilder sb = new StringBuilder(); sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper, this.GetType(), prefix)); bool isClientBlock = htmlHelper.ViewData["ClientBindings"] != null || htmlHelper.ViewContext.HttpContext.Items.Contains("ClientTemplateOn"); sb.Append(string.Format(startScriptFormat, BasicHtmlHelper.IdFromName(BasicHtmlHelper.AddField(this.totalPrefix, "$")), BasicHtmlHelper.JavaScriptDate(min, isClientBlock), BasicHtmlHelper.JavaScriptDate(max, isClientBlock), clientOnChanged != null ? clientOnChanged + " return" : " return;", BasicHtmlHelper.JavaScriptDate(curr), clientMinScript, clientMaxScript, yearCombo ? "true" : "false", dateHidden ? "true" : "false", clientRefreshRegistrations, this.dateInCalendar ? "true" : "false", BasicHtmlHelper.GetAttributesString(attributeExtensions), totalPrefix )); sb.Append(BasicHtmlHelper.TrueValue(htmlHelper, this.totalPrefix, curr)); FormatAttribute[] formatAttributes = pAccesor[typeof(FormatAttribute)] as FormatAttribute[]; if (formatAttributes != null && formatAttributes.Length > 0) { sb.Append(BasicHtmlHelper.FormattedValueString(htmlHelper, this.totalPrefix, formatAttributes[0].HtmlEncode ? htmlHelper.Encode(formatAttributes[0].GetDisplay(curr)) : formatAttributes[0].GetDisplay(curr))); } else { DisplayFormatAttribute[] dFormatAttributes = pAccesor[typeof(DisplayFormatAttribute)] as DisplayFormatAttribute[]; if (dFormatAttributes != null && dFormatAttributes.Length > 0) { sb.Append(BasicHtmlHelper.FormattedValueString(htmlHelper, this.totalPrefix, dFormatAttributes[0].HtmlEncode ? htmlHelper.Encode( new FormatAttribute(dFormatAttributes[0]).GetDisplay(curr)) : new FormatAttribute(dFormatAttributes[0]).GetDisplay(curr))); } } if (format != null) { WriteMonthes(sb, format); } return(sb.ToString()); }
internal string GetChangedAndOldValuesRendering(string prefix, HtmlHelper <TModel> htmlHelper) { StringBuilder sb = new StringBuilder(); sb.Append(BasicHtmlHelper.SafeHidden(htmlHelper, BasicHtmlHelper.AddField(prefix, "$.Item.Changed"), this.Item.Changed).ToString()); if (this.Item != null && this.Item.OldValue != null) { prefix = BasicHtmlHelper.AddField(prefix, "$.Item.OldValue"); if (fieldsToStore != null) { foreach (LambdaExpression l in fieldsToStore) { object val = null; string name = ExpressionHelper.GetExpressionText(l); try { val = l.Compile().DynamicInvoke(this.Item.OldValue); } catch { } if (val is IConvertible || val is Guid) { string res = BasicHtmlHelper.SafeHidden(htmlHelper, BasicHtmlHelper.AddField(prefix, name), val).ToString(); sb.Append(res); } else { MVCControlsToolkit.Controls.Bindings.ModelTranslatorLight model = new MVCControlsToolkit.Controls.Bindings.ModelTranslatorLight(); model.ImportFromModel(val); sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper, typeof(MVCControlsToolkit.Controls.Bindings.ModelTranslatorLight), BasicHtmlHelper.AddField(prefix, name))); string res = BasicHtmlHelper.SafeHidden(htmlHelper, BasicHtmlHelper.AddField(prefix, name + ".$.JSonModel"), model.JSonModel).ToString(); sb.Append(res); } } } else { Stack <object> recursionControl = new Stack <object>(); recursionControl.Push(this.Item.OldValue); addObjectPropertiesTorender(prefix, htmlHelper, sb, propertiesToStore, this.Item.OldValue, recursionControl); recursionControl.Pop(); } } return(sb.ToString()); }
public static MvcHtmlString SplittDateTimeFor <TModel>( this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, Nullable <DateTime> > > expression, bool useTemplate = false, string templateName = null) { var fullPropertyPath = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName( ExpressionHelper.GetExpressionText(expression)); if (expression == null) { throw (new ArgumentNullException("expression")); } Nullable <DateTime> date = null; try { date = expression.Compile().Invoke(htmlHelper.ViewData.Model); } catch { } string Year; string Month; string Day; if (!date.HasValue || date.Value != default(DateTime)) { Year = string.Empty; Month = string.Empty; Day = string.Empty; } else { Year = date.Value.Year.ToString(); Month = date.Value.Month.ToString(); Day = date.Value.Day.ToString(); } TestDateTimeDisplay displayModel = new TestDateTimeDisplay(); displayModel.ImportFromModel(date, null); if (useTemplate) { if (templateName == null) { templateName = typeof(TestDateTimeDisplay).Name; } ViewDataDictionary <TestDateTimeDisplay> dataDictionary = new ViewDataDictionary <TestDateTimeDisplay>(displayModel); dataDictionary.TemplateInfo.HtmlFieldPrefix = fullPropertyPath + ".$"; string res = BasicHtmlHelper.RenderDisplayInfo(htmlHelper, typeof(TestDateTimeDisplay), fullPropertyPath) + htmlHelper.Partial(templateName, dataDictionary); return(MvcHtmlString.Create(res)); } else { string res = BasicHtmlHelper.RenderDisplayInfo(htmlHelper, typeof(TestDateTimeDisplay), fullPropertyPath) + htmlHelper.TextBox(fullPropertyPath + ".$.Year", Year, new { style = "text-align:right" }).ToString() + htmlHelper.ValidationMessage(fullPropertyPath + ".Year", "*") + " " + htmlHelper.TextBox(fullPropertyPath + ".$.Month", Month, new { style = "text-align:right" }).ToString() + htmlHelper.ValidationMessage(fullPropertyPath + ".Month", "*") + " " + htmlHelper.TextBox(fullPropertyPath + ".$.Day", Day, new { style = "text-align:right" }).ToString() + htmlHelper.ValidationMessage(fullPropertyPath + ".Day", "*") + " "; return(MvcHtmlString.Create(res)); } }
public static MvcHtmlString PackedListFor <TModel, TValue>( this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, IEnumerable <TValue> > > expression, string separator, object attributes = null, bool useHidden = false, bool useTemplate = false, object template = null) { var fullPropertyPath = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName( ExpressionHelper.GetExpressionText(expression)); var propertyPath = ExpressionHelper.GetExpressionText(expression); if (expression == null) { throw (new ArgumentNullException("expression")); } if (separator == null) { throw (new ArgumentNullException("separator")); } if (attributes == null) { attributes = new { } } ; IEnumerable <TValue> values = null; try { values = expression.Compile().Invoke(htmlHelper.ViewData.Model); } catch { } if (values == null) { values = new List <TValue>(); } PackedList <IEnumerable <TValue>, TValue> displayModel = new PackedList <IEnumerable <TValue>, TValue>(); displayModel.ImportFromModel(values, new object[] { separator }); StringBuilder sb = new StringBuilder(); sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper, typeof(PackedList <IEnumerable <TValue>, TValue>), propertyPath)); sb.Append(htmlHelper.Hidden(BasicHtmlHelper.AddField(propertyPath, "$.Separator"), separator)).ToString(); if (useTemplate) { if (template == null) { template = typeof(PackedList <IEnumerable <TValue>, TValue>).Name; } ViewDataDictionary <PackedList <IEnumerable <TValue>, TValue> > dataDictionary = new ViewDataDictionary <PackedList <IEnumerable <TValue>, TValue> >(displayModel); dataDictionary.TemplateInfo.HtmlFieldPrefix = BasicHtmlHelper.AddField(fullPropertyPath, "$"); sb.Append(new TemplateInvoker <PackedList <IEnumerable <TValue>, TValue> >(template) .Invoke(htmlHelper, dataDictionary)); } else { if (useHidden) { sb.Append(htmlHelper.Hidden( BasicHtmlHelper.AddField(propertyPath, "$.PackedValue"), displayModel.PackedValue, attributes)); } else { sb.Append(htmlHelper.TextBox( BasicHtmlHelper.AddField(propertyPath, "$.PackedValue"), displayModel.PackedValue, attributes)); } } return(MvcHtmlString.Create(sb.ToString())); } }
public static MvcHtmlString CheckBoxListFor <TModel, TChoiceItem, TValue, TDisplay> ( this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, IEnumerable <TValue> > > expression, ChoiceList <TChoiceItem, TValue, TDisplay> choiceList, bool useTemplate = false, object template = null, object itemTemplate = null) where TValue : IComparable { IEnumerable <TValue> values = default(IEnumerable <TValue>); if (expression == null) { throw (new ArgumentNullException("expression")); } try { values = expression.Compile().Invoke(htmlHelper.ViewData.Model); } catch {} if (values == null) { values = new List <TValue>(); } if (choiceList == null) { throw (new ArgumentNullException("choiceList")); } var fullPropertyPath = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName( ExpressionHelper.GetExpressionText(expression)); var propertyPath = ExpressionHelper.GetExpressionText(expression); CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> displayModel = new CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay>(); displayModel.ImportFromModel(values, new object[] { choiceList }); StringBuilder sb = new StringBuilder(); sb.Append(BasicHtmlHelper.RenderDisplayInfo(htmlHelper, typeof(CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay>), propertyPath)); int index = 0; foreach (CheckBoxListItem <TValue> item in displayModel.Items) { sb.Append(htmlHelper.Hidden (EnumerableHelper.CreateSubIndexName(BasicHtmlHelper.AddField(propertyPath, "$.Items"), index) + ".Code", item.Code)); index++; } if (useTemplate) { if (template == null && itemTemplate == null) { template = typeof(CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay>).Name; } if (template != null) { ViewDataDictionary <CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> > dataDictionary = new ViewDataDictionary <CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> >(displayModel); dataDictionary.TemplateInfo.HtmlFieldPrefix = (BasicHtmlHelper.AddField(fullPropertyPath, "$")); sb.Append(new TemplateInvoker <CheckBoxList <IEnumerable <TValue>, TChoiceItem, TValue, TDisplay> >(template).Invoke <TModel>(htmlHelper, dataDictionary)); } else { int itemIndex = 0; foreach (CheckBoxListItem <TValue> item in displayModel.Items) { ViewDataDictionary <CheckBoxListItem <TValue> > dataDictionary = new ViewDataDictionary <CheckBoxListItem <TValue> >(item); dataDictionary.TemplateInfo.HtmlFieldPrefix = (BasicHtmlHelper.AddField(fullPropertyPath, string.Format("$.Items[{0}]", itemIndex))); sb.Append(new TemplateInvoker <CheckBoxListItem <TValue> >(itemTemplate) .Invoke(htmlHelper, dataDictionary)); itemIndex++; } } } else { index = 0; foreach (CheckBoxListItem <TValue> item in displayModel.Items) { sb.Append("<div>"); sb.Append(htmlHelper.CheckBox( EnumerableHelper.CreateSubIndexName(BasicHtmlHelper.AddField(propertyPath, "$.Items"), index) + ".Selected", item.Selected, item.DisplayAttributes)); sb.Append(" "); sb.Append(string.Format(CultureInfo.InvariantCulture, "<span {0}>{1}</span>", BasicHtmlHelper.GetAttributesString(item.LabelAttributes), item.Label)); sb.Append("</div>"); index++; } } return(MvcHtmlString.Create(sb.ToString())); }
internal static IBindingsBuilder <T> InnerClientViewModel <T>( this HtmlHelper htmlHelper, string uniqueName, T model, string partialPrefix, string prefix, string htmlElementId, bool initialSave, bool applyBindings, bool applyDependencies = true ) where T : class, new() { if (partialPrefix == null) { partialPrefix = string.Empty; } if (prefix == null) { prefix = string.Empty; } if (uniqueName == null) { throw (new ArgumentNullException("uniqueName")); } string validationType = null; switch (MvcEnvironment.Validation(htmlHelper)) { case ValidationType.StandardClient: validationType = "StandardClient"; break; case ValidationType.UnobtrusiveClient: validationType = "UnobtrusiveClient"; break; default: validationType = "Server"; break; } if (model == null) { return(new BindingsBuilder <T>(null, uniqueName, prefix, validationType, null, htmlHelper)); } string script = null; if (applyBindings) { if (htmlElementId == null) { script = rootBingingScript; } else { script = elementBingingScript; } } else if (applyDependencies) { script = modelScript; } else { script = modelScriptNoDep; } ModelTranslator <T> translator = new ModelTranslator <T>(); translator.ImportFromModel(model); htmlHelper.ViewContext.Writer.Write( BasicHtmlHelper.RenderDisplayInfo(htmlHelper, typeof(ModelTranslator <T>), partialPrefix, true)); object ocount = htmlHelper.ViewContext.Controller.ViewData["__ClientModelsCount__"]; int count = 0; if (ocount != null) { count = (int)ocount; } count++; htmlHelper.ViewContext.Controller.ViewData["__ClientModelsCount__"] = count; string jsonHiddenId = BasicHtmlHelper.IdFromName( BasicHtmlHelper.AddField(prefix, "$.JSonModel" + count.ToString())); htmlHelper.ViewContext.Writer.Write( BasicHtmlHelper.SafeHiddenUC(htmlHelper, BasicHtmlHelper.AddField(partialPrefix, "$.JSonModel"), string.Empty, jsonHiddenId ) ); string saveScript = string.Empty; if (initialSave) { saveScript = uniqueName + ".save();"; } string assignement = uniqueName; if (!assignement.Contains('.')) { assignement = "var " + assignement; } string jsonModel = string.Format(script, assignement, uniqueName, translator.JSonModel, saveScript, htmlElementId, jsonHiddenId, validationType); htmlHelper.ViewContext.Writer.Write(jsonModel); IBindingsBuilder <T> result = new BindingsBuilder <T>(htmlHelper.ViewContext.Writer, uniqueName, prefix, validationType, jsonHiddenId, htmlHelper); /* result.AddMethod("save", string.Format(@" * function(){{ * document.getElementById('{0}').value = ko.mapping.toJSON(this); * }}", * jsonHiddenId)); * result.AddMethod("validateAndSave", string.Format(@" * function(){{ * if(MvcControlsToolkit_FormIsValid('{0}', '{1}')){{ * document.getElementById('{0}').value = ko.mapping.toJSON(this); * return true; * }} * return false; * }}", * jsonHiddenId, validationType)); * result.AddMethod("saveAndSubmit", string.Format(@" * function(){{ * if(this.validateAndSave()){{ * $('#{0}').parents('form').submit(); * }} * }}", * jsonHiddenId * )); * result.AddMethod("saveAndSubmitAlone", string.Format(@" * function(formId){{ * if(MvcControlsToolkit_FormIsValid(formId, '{1}')){{ * this.save(); * $('#{0}').parents('form').submit(); * }} * }}", * jsonHiddenId, * validationType * )); */ return(result); }