示例#1
0
 protected virtual void Dispose(bool disposing)
 {
     // If you need thread safety, use a lock around these
     // operations, as well as in your methods that use the resource.
     if (!_disposed)
     {
         if (disposing)
         {
             string res = new KoAutomaticBinder <T>((htmlHelper.ViewContext.Writer as StringWriter).ToString(),
                                                    bindings,
                                                    prefix).ToString();
             writer.Write(res);
             htmlHelper.ViewContext.Writer = writer;
             htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
             htmlHelper.ViewData.Remove("ClientBindings");
         }
         _disposed = true;
     }
 }
        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);
        }