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); }
public static HtmlHelper <F> _foreach <T, F>( this HtmlHelper <T> htmlHelper, Expression <Func <T, IEnumerable <F> > > expression, ExternalContainerType itemsContainer = ExternalContainerType.koComment, object htmlAttributes = null, string afterAdd = null, string beforeRemove = null, string afterRender = null, string afterAllRender = null ) where F : class { if (expression == null) { throw (new ArgumentNullException("expression")); } IDictionary <string, object> attributes = null; if (htmlAttributes == null) { attributes = new RouteValueDictionary(); } else if (htmlAttributes is IDictionary <string, object> ) { attributes = htmlAttributes as IDictionary <string, object>; } else { attributes = new RouteValueDictionary(htmlAttributes); } IBindingsBuilder <T> bindings = htmlHelper.ClientBindings(); if (bindings == null) { throw (new ArgumentNullException("bindings")); } F model = default(F); Type basicType = typeof(F); if (basicType.IsClass) { var constructor = basicType.GetConstructor(new Type[0]); if (constructor != null) { model = (F)constructor.Invoke(new object[0]); } } string bindingFieldName = bindings.GetFullBindingName(expression); string fieldName = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); bool nested; int hl = helperLevel(htmlHelper, out nested); string templateSymbol = ClientTemplateHelper.templateSymbol + hl.ToString(); IBindingsBuilder <F> contentBindings = new BindingsBuilder <F>(htmlHelper.ViewContext.Writer, string.Empty, templateSymbol + ".A", bindings.ValidationType, null, htmlHelper); ViewDataDictionary <F> dic = new ViewDataDictionary <F>(model); dic["ClientBindings"] = contentBindings; dic["_TemplateLevel_"] = hl; HttpContext.Current.Items["_TemplateLevel_" + hl.ToString()] = contentBindings; dic.TemplateInfo.HtmlFieldPrefix = templateSymbol + ".A"; HtmlHelper <F> newHelper = new TemplateInvoker <F>().BuildHelper(htmlHelper, dic); string bindingsValue = string.Format( forScript, bindingFieldName, fieldName, BasicHtmlHelper.IdFromName(fieldName), templateSymbol, bindings.ValidationType == "UnobtrusiveClient" ? "true" : "false", afterAdd == null ? "null" : afterAdd, beforeRemove == null ? "null" : beforeRemove, afterRender == null ? "null" : afterRender, afterAllRender == null ? "null" : afterAllRender ); if (attributes.ContainsKey("data-bind")) { attributes["data-bind"] = (attributes["data-bind"] as string) + ", " + bindingsValue; } else { attributes["data-bind"] = bindingsValue; } attributes["data-nobinding"] = "true"; if (!nested && bindings.ValidationType == "UnobtrusiveClient") { bindings.AddServerErrors(fieldName); } string openTag; string closeTag; BasicHtmlHelper.GetContainerTags(itemsContainer, attributes, out openTag, out closeTag); htmlPush("_ClientControlsFlowStartStack_", openTag); htmlPush("_ClientControlsFlowStack_", closeTag); string innerHtml = string.Empty; return(newHelper); }
public static MvcHtmlString ClientTemplate <T>(this HtmlHelper htmlHelper, string uniqueName, object template, bool?applyAutomaticBindings = null, T prototype = null) where T : class { if (string.IsNullOrWhiteSpace(uniqueName)) { throw new ArgumentNullException("uniqueName"); } if (template == null) { throw new ArgumentNullException("template"); } htmlHelper.ViewContext.HttpContext.Items["ClientTemplateOn"] = new object(); bool oldValidation = htmlHelper.ViewContext.ClientValidationEnabled; if (!MvcEnvironment.UnobtrusiveAjaxOn(htmlHelper)) { htmlHelper.ViewContext.ClientValidationEnabled = false; } FormContext oldFormContext = htmlHelper.ViewContext.FormContext; htmlHelper.ViewContext.FormContext = new FormContext(); string validationType = null; bool automaticBindings = false; if (applyAutomaticBindings.HasValue) { automaticBindings = applyAutomaticBindings.Value; } else { automaticBindings = htmlHelper.ViewData["ClientBindings"] != null; } switch (MvcEnvironment.Validation(htmlHelper)) { case ValidationType.StandardClient: validationType = "StandardClient"; break; case ValidationType.UnobtrusiveClient: validationType = "UnobtrusiveClient"; break; default: validationType = "Server"; break; } IBindingsBuilder <T> bindings = new BindingsBuilder <T>(htmlHelper.ViewContext.Writer, string.Empty, templateSymbol + "0.A", validationType, null, htmlHelper); string stringTemplate = new TemplateInvoker <T>(template, bindings).InvokeVirtual(htmlHelper, templateSymbol + "0.A"); if (automaticBindings) { stringTemplate = new KoAutomaticBinder <T>(stringTemplate, bindings).ToString(); } else { stringTemplate = new KoAutomaticBinder <T>(stringTemplate, null).ToString(); } /* stringTemplate = stringTemplate.Replace(templateSymbol + ".A", "${MvcControlsToolkit_TemplateName($item)}") * .Replace(templateSymbol + "_A", "${MvcControlsToolkit_TemplateId($item)}");*/ /* stringTemplate = stringTemplate.Replace(templateSymbol + ".A", templateSymbol+"0.A") * .Replace(templateSymbol + "_A", templateSymbol+"0_A"); */ string globalEvals = string.Empty; if (htmlHelper.ViewContext.HttpContext.Items.Contains("GlobalEvalsRequired")) { StringBuilder globalEvalsBuilder = htmlHelper.ViewContext.HttpContext.Items["GlobalEvalsRequired"] as StringBuilder; globalEvals = globalEvalsBuilder.ToString(); htmlHelper.ViewContext.HttpContext.Items.Remove("GlobalEvalsRequired"); } string prototypeDeclaration = string.Empty; if (prototype != null) { ModelTranslator <T> model = new ModelTranslator <T>(); model.ImportFromModel(prototype); prototypeDeclaration = string.Format(prototypeFormat, uniqueName, model.JSonModel); } MvcHtmlString res = MvcHtmlString.Create(string.Format(clientTemplateScript, uniqueName, stringTemplate, globalEvals, prototypeDeclaration )); htmlHelper.ViewContext.FormContext = oldFormContext; htmlHelper.ViewContext.ClientValidationEnabled = oldValidation; htmlHelper.ViewContext.HttpContext.Items.Remove("ClientTemplateOn"); return(res); }