public static IBindingsBuilder <T> Template <T, F>( this IBindingsBuilder <T> bindingsBuilder, string templateName, Expression <Func <T, F> > expression, string afterAdd = null, string beforeRemove = null, string afterRender = null, object templateOptions = null, string prefix = null, bool applyClientValidation = true, bool fastNoJavaScript = false, string afterAllRender = null, string templateEngine = null) where T : class { if (expression == null) { throw (new ArgumentNullException("expression")); } if (string.IsNullOrWhiteSpace(templateName)) { throw (new ArgumentNullException("templateName")); } string format = null; string actualPrefix = BasicHtmlHelper.AddField( bindingsBuilder.ModelPrefix, ExpressionHelper.GetExpressionText(expression)); if (prefix == null) { prefix = actualPrefix; } StringBuilder sb = new StringBuilder(); if (templateName[0] != '@') { sb.Append("template: { name: ""); sb.Append(templateName); if (typeof(IEnumerable).IsAssignableFrom(typeof(F))) { sb.Append("", foreach: "); } else { sb.Append("", data: "); } } else { templateName = templateName.Substring(1); sb.Append("template: { name: "); sb.Append(templateName); if (typeof(IEnumerable).IsAssignableFrom(typeof(F))) { sb.Append(", foreach: "); } else { sb.Append(", data: "); } } sb.Append(bindingsBuilder.GetFullBindingName(expression)); if (afterRender != null) { sb.Append(", afterRender: "); sb.Append(afterRender); } if (afterAdd != null) { sb.Append(", afterAdd: "); sb.Append(afterAdd); } if (beforeRemove != null) { sb.Append(", beforeRemove: "); sb.Append(beforeRemove); } if (afterAllRender != null) { sb.Append(", afterAllRender: "); sb.Append(afterAllRender); } if (templateEngine != null) { sb.Append(", templateEngine: ko."); sb.Append(templateEngine); sb.Append(".instance"); } var additionalOptions = new { ModelPrefix = """ + prefix + """, ModelId = """ + BasicHtmlHelper.IdFromName(prefix) + """, ItemPrefix = """", templateSymbol = """ + ClientTemplateHelper.templateSymbol + "0"" }; sb.Append(", templateOptions: {"); if (templateOptions != null) { sb.Append(BasicHtmlHelper.TranslateAnonymous(templateOptions)); sb.Append(", "); } sb.Append(BasicHtmlHelper.TranslateAnonymous(additionalOptions)); sb.Append(" }"); sb.Append(", processingOptions: {"); if (bindingsBuilder.ValidationType == "UnobtrusiveClient") { sb.Append("unobtrusiveClient: true"); if (bindingsBuilder.GetHelper().ViewData["_TemplateLevel_"] == null) { bindingsBuilder.AddServerErrors(actualPrefix); } } else { sb.Append("unobtrusiveClient: false"); } sb.Append(fastNoJavaScript ? ", fastNoJavaScript: true" : ", fastNoJavaScript: false"); sb.Append(applyClientValidation ? ", applyClientValidation: true" : ", applyClientValidation: false"); sb.Append(" }"); sb.Append(" }"); format = sb.ToString(); bindingsBuilder.Add( format ); return(bindingsBuilder); }
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); }