private static void getPropertiesToUpdate(StringBuilder sb, string prefix, PropertyInfo[] propertiesToUpdate, Stack <Type> recursionControl) { for (int i = 0; i < propertiesToUpdate.Length; i++) { if (i > 0) { sb.Append(','); } if (typeof(IConvertible).IsAssignableFrom(propertiesToUpdate[i].PropertyType)) { sb.Append(BasicHtmlHelper.IdFromName(BasicHtmlHelper.AddField(prefix, propertiesToUpdate[i].Name))); } else if (!typeof(System.Collections.IEnumerable).IsAssignableFrom(propertiesToUpdate[i].PropertyType)) { bool recursion = false; foreach (Type type in recursionControl) { if (type == propertiesToUpdate[i].PropertyType) { recursion = true; } } if (recursion) { continue; } recursionControl.Push(propertiesToUpdate[i].PropertyType); getPropertiesToUpdate( sb, BasicHtmlHelper.AddField(prefix, propertiesToUpdate[i].Name), BasicHtmlHelper.GetPropertiesForInput(propertiesToUpdate[i].PropertyType), recursionControl); recursionControl.Pop(); } } }
private void addObjectPropertiesTorender(string prefix, HtmlHelper <TModel> htmlHelper, StringBuilder sb, PropertyInfo[] propertiesToStore, object obj, Stack <object> recursionControl) { foreach (PropertyInfo pi in propertiesToStore) { if (pi.GetIndexParameters().GetLength(0) > 0) { continue; } object val = pi.GetValue(obj, new object[0]); if (val is IConvertible) { string res = BasicHtmlHelper.SafeHidden(htmlHelper, BasicHtmlHelper.AddField(prefix, pi.Name), val).ToString(); sb.Append(res); } else if (val is IEnumerable) { bool recursion = false; foreach (object ob in recursionControl) { if (object.ReferenceEquals(val, ob)) { recursion = true; } ; } if (recursion) { continue; } recursionControl.Push(val); addIEnumerablePropertiesTorender( BasicHtmlHelper.AddField(prefix, pi.Name), htmlHelper, sb, val as IEnumerable, recursionControl); recursionControl.Pop(); } else if (val != null) { bool recursion = false; foreach (object ob in recursionControl) { if (object.ReferenceEquals(val, ob)) { recursion = true; } } if (recursion) { continue; } recursionControl.Push(val); addObjectPropertiesTorender( BasicHtmlHelper.AddField(prefix, pi.Name), htmlHelper, sb, BasicHtmlHelper.GetPropertiesForInput(val.GetType()), val, recursionControl); recursionControl.Pop(); } } }
private void addIEnumerablePropertiesTorender( string prefix, HtmlHelper <TModel> htmlHelper, StringBuilder sb, IEnumerable en, Stack <object> recursionControl) { int i = 0; foreach (object obj in en) { if (obj == null) { i++; continue; } if (obj is IEnumerable) { bool recursion = false; foreach (object ob in recursionControl) { if (object.ReferenceEquals(obj, ob)) { recursion = true; } } if (recursion) { continue; } recursionControl.Push(obj); addIEnumerablePropertiesTorender( string.Format("{0}[{1}]", prefix, i), htmlHelper, sb, obj as IEnumerable, recursionControl); recursionControl.Pop(); } else if (obj is IConvertible) { string res = BasicHtmlHelper.SafeHidden(htmlHelper, string.Format("{0}[{1}]", prefix, i), obj).ToString(); sb.Append(res); i++; } else { bool recursion = false; foreach (object ob in recursionControl) { if (object.ReferenceEquals(obj, ob)) { recursion = true; } } if (recursion) { continue; } recursionControl.Push(obj); addObjectPropertiesTorender( string.Format("{0}[{1}]", prefix, i), htmlHelper, sb, BasicHtmlHelper.GetPropertiesForInput(obj.GetType()), obj, recursionControl); recursionControl.Pop(); } } }
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()); } }