public static void DetailFormFor <VM, TItem>( this HtmlHelper <VM> htmlHelper, AjaxHelper <VM> ajax, RenderInfo <IEnumerable <Tracker <TItem> > > renderInfo, ExternalContainerType externalContainerType, string postActionName, string postControllerName, string detailPrefix = null, string changedFieldCss = null, string deletedRecordCss = null, IDictionary <string, object> externalContainerHtmlattributes = null, string clientOnSuccessEdit = null, string clientOnFailureEdit = null, string clientOnSuccessDisplay = null, string clientOnFailureDisplay = null, string loadingElementId = null, Dialog detailDialog = null ) where TItem : class, new() { if (renderInfo == null) { throw (new ArgumentNullException("renderInfo")); } if (ajax == null) { throw (new ArgumentNullException("ajax")); } if (postActionName == null) { throw (new ArgumentNullException("postActionName")); } if (postControllerName == null) { throw (new ArgumentNullException("postControllerName")); } if (externalContainerHtmlattributes == null) { externalContainerHtmlattributes = new Dictionary <string, object>(); } string baseName = BasicHtmlHelper.IdFromName(renderInfo.Prefix); string formName = baseName + "_AjaxForm"; string containerName = baseName + "_Container"; string onSuccessName = baseName + "_OnSuccess"; string onFailureName = baseName + "_OnFailure"; string onBeginName = baseName + "_OnBegin"; string beginTag; string closeTag; externalContainerHtmlattributes["id"] = containerName; BasicHtmlHelper.GetContainerTags(externalContainerType, externalContainerHtmlattributes, out beginTag, out closeTag); string javascriptChangedFieldCss = changedFieldCss == null ? "null" : "'" + changedFieldCss + "'"; string javascriptDeletedRecordCss = deletedRecordCss == null ? "null" : "'" + deletedRecordCss + "'"; string javascriptClientOnSuccessEdit = clientOnSuccessEdit == null ? "null" : "'" + clientOnSuccessEdit + "'"; string javascriptClientOnSuccessDisplay = clientOnSuccessDisplay == null ? "null" : "'" + clientOnSuccessDisplay + "'"; string javascriptClientOnFailureEdit = clientOnFailureEdit == null ? "null" : "'" + clientOnFailureEdit + "'"; string javascriptClientOnFailureDisplay = clientOnFailureDisplay == null ? "null" : "'" + clientOnFailureDisplay + "'"; if (detailPrefix == null) { detailPrefix = string.Empty; } string validationType = null; string unobtrusiveAjaxOn = "false"; if (MvcEnvironment.UnobtrusiveAjaxOn(htmlHelper)) { unobtrusiveAjaxOn = "true"; } switch (MvcEnvironment.Validation(htmlHelper)) { case ValidationType.StandardClient: validationType = "StandardClient"; break; case ValidationType.UnobtrusiveClient: validationType = "UnobtrusiveClient"; break; default: validationType = "Server"; break; } htmlHelper.ViewContext.Writer.Write( string.Format(prefixTranslationScript, baseName, BasicHtmlHelper.IdFromName(detailPrefix))); PropertyInfo[] propertiesToUpdate = BasicHtmlHelper.GetPropertiesForInput(typeof(TItem)); StringBuilder sb = new StringBuilder(); Stack <Type> recursionControl = new Stack <Type>(); recursionControl.Push(typeof(TItem)); getPropertiesToUpdate( sb, string.Empty, propertiesToUpdate, recursionControl); recursionControl.Pop(); object formHtmlAttributes = null; if (formName != null) { formHtmlAttributes = new { id = formName }; } htmlHelper.ViewContext.Writer.Write( string.Format(fieldsToUpdateScript, baseName, sb.ToString(), javascriptChangedFieldCss, javascriptDeletedRecordCss)); htmlHelper.ViewContext.Writer.Write( string.Format(onBeginScript, onBeginName, baseName, "Edit", "null", loadingElementId, validationType)); string dialogOpen; if (detailDialog == null) { dialogOpen = string.Empty; } else { dialogOpen = detailDialog.GetShow('#' + formName); } htmlHelper.ViewContext.Writer.Write( string.Format(onSuccessScript, onSuccessName, baseName, javascriptClientOnSuccessDisplay, javascriptClientOnSuccessEdit, dialogOpen, formName, validationType, unobtrusiveAjaxOn, containerName)); htmlHelper.ViewContext.Writer.Write( string.Format(onFailureScript, onFailureName, baseName, javascriptClientOnFailureDisplay, javascriptClientOnFailureEdit)); if (detailDialog != null) { htmlHelper.ViewContext.Writer.Write(detailDialog.GetCreation('#' + formName)); } using (var form = ajax.BeginForm( postActionName, postControllerName, new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = unobtrusiveAjaxOn == "true" ? null : containerName, LoadingElementId = loadingElementId, OnSuccess = onSuccessName, OnBegin = onBeginName, OnFailure = onFailureName, InsertionMode = InsertionMode.Replace }, formHtmlAttributes)) { htmlHelper.ViewContext.Writer.Write(beginTag); htmlHelper.ViewContext.Writer.Write(closeTag); } if (!MvcEnvironment.UnobtrusiveAjaxOn(htmlHelper)) { htmlHelper.ViewContext.Writer.Write( htmlHelper.AjaxSubmitEnabler(formName, false).ToString()); } }
public static MvcHtmlString DetailLink <TItem>( this HtmlHelper <TItem> htmlHelper, AjaxHelper ajax, string text, DetailType detailType, string controllerAction, string controllerName, object parameters, string loadingElementId, IDictionary <string, object> htmlAttributes = null) { if (ajax == null) { throw (new ArgumentNullException("ajax")); } if (text == null) { throw (new ArgumentNullException("text")); } if (controllerAction == null) { throw (new ArgumentNullException("ControllerAction")); } if (controllerName == null) { throw (new ArgumentNullException("ControllerName")); } string status = null; bool visible = true; if (htmlHelper.ViewData.ContainsKey("status")) { status = htmlHelper.ViewData["status"] as string; } string detail = "Display"; if (status == "InsertItem") { if (detailType == DetailType.Edit) { detail = "Insert"; } else { visible = false; } } else if (status == "Insert" || status == "Phantom") { visible = false; } else if (detailType == DetailType.Edit) { detail = "FirstEdit"; } if (!visible) { return(MvcHtmlString.Create(string.Empty)); } string rootItemName = BasicHtmlHelper.IdFromName(htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix); string baseName = rootItemName.Substring(0, rootItemName.LastIndexOf("___")); baseName = baseName.Substring(0, baseName.LastIndexOf("___")); string containerName = baseName + "_Container"; string onSuccessName = baseName + "_OnSuccess"; string onFailureName = baseName + "_OnFailure"; string onBeginScript = MvcEnvironment.UnobtrusiveAjaxOn(htmlHelper) ? "return OnBeginDetailForm('{0}', '{1}', '{2}', '{3}', null);" : "function() {{ return OnBeginDetailForm('{0}', '{1}', '{2}', '{3}', null); }}"; return(ajax.ActionLink(text, controllerAction, controllerName, parameters, new AjaxOptions() { HttpMethod = "Get", UpdateTargetId = containerName, LoadingElementId = loadingElementId, OnSuccess = onSuccessName, OnBegin = string.Format( onBeginScript, baseName, detail, rootItemName, loadingElementId), OnFailure = onFailureName, InsertionMode = InsertionMode.Replace }, htmlAttributes)); }
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); }