public void FormWithTargetId() { // Arrange AjaxHelper ajaxHelper = GetAjaxHelper(); AjaxOptions ajaxOptions = new AjaxOptions { UpdateTargetId = "some-id" }; StringWriter writer = new StringWriter(); ajaxHelper.ViewContext.Writer = writer; // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", ajaxOptions); // Assert Assert.AreEqual(AjaxFormWithTargetId, writer.ToString()); }
public void FormAnonymousValues() { // Arrange Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>(MockBehavior.Strict); AjaxHelper ajaxHelper = GetAjaxHelper(mockResponse); AjaxOptions ajaxOptions = GetEmptyOptions(); object values = new { controller = "Controller" }; // Arrange expectations mockResponse.Expect(response => response.Write(AjaxFormWithEmptyOptions)).Verifiable(); // Act IDisposable form = ajaxHelper.BeginForm("Action", values, ajaxOptions); // Assert mockResponse.Verify(); }
public void FormControllerAnonymousValuesAndAttributes() { // Arrange AjaxHelper ajaxHelper = GetAjaxHelper(); AjaxOptions ajaxOptions = GetEmptyOptions(); object values = new { id = 5 }; object htmlAttributes = new { method = "get" }; StringWriter writer = new StringWriter(); ajaxHelper.ViewContext.Writer = writer; // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", values, ajaxOptions, htmlAttributes); // Assert Assert.AreEqual(AjaxFormWithIdAndHtmlAttributes, writer.ToString()); }
public void FormWithTargetId() { // Arrange Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>(MockBehavior.Strict); AjaxHelper ajaxHelper = GetAjaxHelper(mockResponse); AjaxOptions ajaxOptions = new AjaxOptions { UpdateTargetId = "some-id" }; // Arrange expectations mockResponse.Expect(response => response.Write(AjaxFormWithTargetId)).Verifiable(); // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", ajaxOptions); // Assert mockResponse.Verify(); }
public void FormControllerTypedValues() { // Arrange AjaxHelper ajaxHelper = GetAjaxHelper(); AjaxOptions ajaxOptions = GetEmptyOptions(); RouteValueDictionary values = new RouteValueDictionary { { "id", 5 } }; StringWriter writer = new StringWriter(); ajaxHelper.ViewContext.Writer = writer; // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", values, ajaxOptions); // Assert Assert.AreEqual(AjaxFormWithId, writer.ToString()); }
public void FormControllerAnonymousValuesAndAttributes() { // Arrange Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>(MockBehavior.Strict); AjaxHelper ajaxHelper = GetAjaxHelper(mockResponse); AjaxOptions ajaxOptions = GetEmptyOptions(); object values = new { id = 5 }; object htmlAttributes = new { method = "get" }; // Arrange expectations mockResponse.Expect(response => response.Write(AjaxFormWithIdAndHtmlAttributes)).Verifiable(); // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", values, ajaxOptions, htmlAttributes); // Assert mockResponse.Verify(); }
public void FormControllerTypedValues() { // Arrange Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>(MockBehavior.Strict); AjaxHelper ajaxHelper = GetAjaxHelper(mockResponse); AjaxOptions ajaxOptions = GetEmptyOptions(); RouteValueDictionary values = new RouteValueDictionary { { "id", 5 } }; // Arrange expectations mockResponse.Expect(response => response.Write(AjaxFormWithId)).Verifiable(); // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", values, ajaxOptions); // Assert mockResponse.Verify(); }
public void BeginFormSetsAndRestoresFormContext() { // Arrange AjaxHelper ajaxHelper = GetAjaxHelper(); FormContext originalContext = new FormContext(); ajaxHelper.ViewContext.FormContext = originalContext; // Act & assert - push MvcForm theForm = ajaxHelper.BeginForm(new AjaxOptions()); Assert.IsNotNull(ajaxHelper.ViewContext.FormContext); Assert.AreNotEqual(originalContext, ajaxHelper.ViewContext.FormContext, "FormContext should have been set to a new instance."); // Act & assert - pop theForm.Dispose(); Assert.AreEqual(originalContext, ajaxHelper.ViewContext.FormContext, "FormContext was not properly restored."); }
private MvcForm GenerateForm(AjaxHelper helper, string actionName, string areaName, object routeValues, object htmlAttributes, string formBody) { var form = helper.BeginForm( actionName, ControllerName, HtmlHelperExtension.AddRouteValue(routeValues, new { area = areaName }), new AjaxOptions { HttpMethod = "POST" }, HtmlHelperExtension.ConvertHtmlAttributesToIDictionary(htmlAttributes)); var htmlHelper = new HtmlHelper(helper.ViewContext, helper.ViewDataContainer); string token = htmlHelper.AntiForgeryToken().ToHtmlString(); helper.ViewContext.Writer.Write(token + formBody); return(form); }
public void FormAnonymousValuesAndAttributes() { // Arrange AjaxHelper ajaxHelper = GetAjaxHelper(); AjaxOptions ajaxOptions = new AjaxOptions { UpdateTargetId = "some-id" }; object values = new { controller = "Controller" }; object htmlAttributes = new { method = "get" }; StringWriter writer = new StringWriter(); ajaxHelper.ViewContext.Writer = writer; // Act IDisposable form = ajaxHelper.BeginForm("Action", values, ajaxOptions, htmlAttributes); // Assert Assert.AreEqual(AjaxFormWithHtmlAttributes, writer.ToString()); }
public void FormControllerTypedValuesAndAttributes() { // Arrange AjaxHelper ajaxHelper = GetAjaxHelper(); AjaxOptions ajaxOptions = GetEmptyOptions(); RouteValueDictionary values = new RouteValueDictionary { { "id", 5 } }; Dictionary <string, object> htmlAttributes = new Dictionary <string, object> { { "method", "get" } }; StringWriter writer = new StringWriter(); ajaxHelper.ViewContext.Writer = writer; // Act IDisposable form = ajaxHelper.BeginForm("Action", "Controller", values, ajaxOptions, htmlAttributes); // Assert Assert.AreEqual(AjaxFormWithIdAndHtmlAttributes, writer.ToString()); }
public void FormTypedValuesAndAttributes() { // Arrange Mock <HttpResponseBase> mockResponse = new Mock <HttpResponseBase>(MockBehavior.Strict); AjaxHelper ajaxHelper = GetAjaxHelper(mockResponse); AjaxOptions ajaxOptions = new AjaxOptions { UpdateTargetId = "some-id" }; RouteValueDictionary values = new RouteValueDictionary { { "controller", "Controller" } }; Dictionary <string, object> htmlAttributes = new Dictionary <string, object> { { "method", "get" } }; // Arrange expectations mockResponse.Expect(response => response.Write(AjaxFormWithHtmlAttributes)).Verifiable(); // Act IDisposable form = ajaxHelper.BeginForm("Action", values, ajaxOptions, htmlAttributes); // Assert mockResponse.Verify(); }
public MvcForm BeginForm(AjaxHelper ajaxHelper) { return(ajaxHelper.BeginForm(ActionName, ControllerName, RouteValues, AjaxOptions, HtmlAttributes)); }
public MvcForm BeginForm(AjaxOptions ajaxOptions) { return(_helper.BeginForm(ajaxOptions)); }
public static MvcForm SecureBeginForm(this AjaxHelper ajaxHelper, ActionResult result, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes) { var callInfo = result.GetT4MVCResult(); return(ajaxHelper.BeginForm(callInfo.Action, callInfo.Controller, callInfo.RouteValueDictionary, ajaxOptions, htmlAttributes)); }
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 MvcForm BeginFormWithId(this AjaxHelper ajaxHelper, string formid, AjaxOptions ajaxOptions) { return(ajaxHelper.BeginForm(null, null, ajaxOptions, new { id = formid })); }
public static MvcForm BeginFormWithCloseDialog(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, string errorPanel, object htmlAttributes) { ProcessOnComplete(ajaxOptions); return(ajaxHelper.BeginForm(actionName, controllerName, routeValues, ajaxOptions, errorPanel, htmlAttributes)); }