private static MvcForm FormHelper(this AjaxHelper ajaxHelper, string formId, string formAction, bool isAjax, string eventName, string updateId, string cssClassNames, IDictionary<string, object> htmlAttributes) { var tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("action", formAction); tagBuilder.MergeAttribute("method", @"post"); var metadataBuilder = new System.Text.StringBuilder(); metadataBuilder.AppendFormat("IsAjax: {0}", isAjax.ToJavascriptString()); if(!string.IsNullOrEmpty(eventName)) metadataBuilder.AppendFormat(", EventName: '{0}'", eventName); if (!string.IsNullOrEmpty(updateId)) metadataBuilder.AppendFormat(", UpdateId: '{0}'", updateId); var metadata = "{" + metadataBuilder.ToString() + "}"; tagBuilder.MergeAttribute("autocomplete", @"off"); tagBuilder.MergeAttribute("class", "jqAjaxForm " + (!string.IsNullOrEmpty(cssClassNames) ? cssClassNames : string.Empty) + @" " + metadata); tagBuilder.GenerateId(formId); ajaxHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(ajaxHelper.ViewContext); if (ajaxHelper.ViewContext.ClientValidationEnabled) ajaxHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"]; return form; }
private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary <string, object> htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); // action is implicitly generated, so htmlAttributes take precedence. tagBuilder.MergeAttribute("action", formAction); // method is an explicit parameter, so it takes precedence over the htmlAttributes. tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); bool traditionalJavascriptEnabled = htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled; if (traditionalJavascriptEnabled) { // forms must have an ID for client validation tagBuilder.GenerateId(htmlHelper.ViewContext.FormIdGenerator()); } htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); MvcForm theForm = new MvcForm(htmlHelper.ViewContext); if (traditionalJavascriptEnabled) { htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"]; } return(theForm); }
private void Init(HtmlHelper html, string actionName = null, string controllerName = null, object routeValues = null, FormMethod method = FormMethod.Post, object htmlAttrs = null, FormType? formType = null) { var attrs = new HtmlAttributes(htmlAttrs); if (formType != null) attrs["class"] += "form-" + formType.ToString().ToLower(); if (html == null) return; _form = html.BeginForm(actionName, controllerName, new RouteValueDictionary(routeValues), method, attrs.ToDictionary()); }
public ExampleConfigurator PostTo(string action, string controller) { string theme = this.htmlHelper.ViewContext.HttpContext.Request.Params["theme"] ?? "vista"; this.form = this.htmlHelper.BeginForm(action, controller, new { theme = theme }); return this; }
public static MvcForm BeginTokenForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes) { MvcForm form = htmlHelper.BeginForm(actionName, controllerName, method, htmlAttributes); MvcHtmlString csrf = htmlHelper.CsrfToken(); htmlHelper.ViewContext.Writer.Write(csrf.ToHtmlString()); return(form); }
/// <summary> /// /// </summary> /// <param name="html"></param> /// <param name="formId"></param> /// <returns></returns> public static IDisposable BeginAjaxContentValidation( this HtmlHelper html, string formId) { MvcForm form = null; if (html.ViewContext.FormContext == null) { html.EnableClientValidation(); form = new MvcForm(html.ViewContext); html.ViewContext.FormContext.FormId = formId; } return new AjaxContentValidation(html.ViewContext, form); }
public void EndFormRendersCloseFormTag() { // Arrange Mock<HttpResponseBase> mockHttpResponse = GetHttpResponseForForm(); MvcForm form = new MvcForm(mockHttpResponse.Object); // Act form.EndForm(); // Assert mockHttpResponse.Verify(); }
/// <summary> /// HTML Form wrapper /// </summary> /// <param name="startTags">html content injected after begin form</param> /// <param name="endTags">html content injected before end form</param> public BsMvcForm(ViewContext viewContext, MvcForm form, string startTags = null, string endTags = null) { _form = form; _endTags = endTags; _viewContext = viewContext; if (!string.IsNullOrEmpty(startTags)) { _viewContext.Writer.Write(startTags); } }
public void DisposeTwiceRendersCloseFormTagOnce() { // Arrange Mock<HttpResponseBase> mockHttpResponse = GetHttpResponseForForm(); MvcForm form = new MvcForm(mockHttpResponse.Object); // Act form.Dispose(); form.Dispose(); // Assert mockHttpResponse.Verify(); }
public void EndFormRendersCloseFormTag() { // Arrange StringWriter writer = new StringWriter(); ViewContext viewContext = GetViewContext(writer); MvcForm form = new MvcForm(viewContext); // Act form.EndForm(); // Assert Assert.AreEqual("</form>", writer.ToString()); }
private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) { var tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); // action is implicitly generated, so htmlAttributes take precedence. tagBuilder.MergeAttribute("action", formAction); // method is an explicit parameter, so it takes precedence over the htmlAttributes. tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); var theForm = new MvcForm(htmlHelper.ViewContext); return theForm; }
public static MvcForm BeginSecureForm(this HtmlHelper htmlHelper, string actionName, string controllerName) { TagBuilder tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttribute("action", UrlHelper.GenerateUrl(null, actionName, controllerName, new RouteValueDictionary(), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true)); tagBuilder.MergeAttribute("method", "POST", true); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); htmlHelper.ViewContext.Writer.Write(htmlHelper.AntiForgeryToken().ToHtmlString()); var theForm = new MvcForm(htmlHelper.ViewContext); return(theForm); }
public static MvcForm jQueryBeginForm(this AjaxHelper ajaxHelper) { string url = HttpContext.Current.Request.RawUrl; TagBuilder formBuilder = new TagBuilder("form"); Dictionary<string, object> htmlAttributes = new Dictionary<string, object>(); htmlAttributes.Add("onsubmit", "$(this).ajaxSubmit(" + "{"+ " replaceTarget: true, " + " target: $(this), " + "});" + "return false;"); formBuilder.MergeAttributes<string, object>(htmlAttributes); formBuilder.MergeAttribute("action", url); formBuilder.MergeAttribute("method", FormMethod.Post.ToString(), true); ajaxHelper.ViewContext.Writer.Write(formBuilder.ToString(TagRenderMode.StartTag)); MvcForm form = new MvcForm(ajaxHelper.ViewContext); return form; }
public static MvcForm BeginSecureForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("form"); Dictionary<string, object> htmlAttributesDictionary = new Dictionary<string, object>(); foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes)) htmlAttributesDictionary.Add(property.Name, property.GetValue(htmlAttributes)); tagBuilder.MergeAttributes(htmlAttributesDictionary); tagBuilder.MergeAttribute("action", UrlHelper.GenerateUrl(null, actionName, controllerName, new RouteValueDictionary(), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true)); tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); htmlHelper.ViewContext.Writer.Write(htmlHelper.AntiForgeryToken().ToHtmlString()); var theForm = new MvcForm(htmlHelper.ViewContext); return theForm; }
private static IDisposable FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) { var builder = new TagBuilder("form"); builder.MergeAttributes<string, object>(htmlAttributes); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); bool flag = htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled; if (flag) { builder.GenerateId(htmlHelper.DefaultFormIdGenerator()); } htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(htmlHelper.ViewContext); if (flag) { htmlHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return form; }
public static MvcForm BeginForm(this HtmlHelper htmlHelper, IUrl url, FormMethod method, IDictionary<string, object> htmlAttributes) { var builder = new TagBuilder("form"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("action", url.ToString()); builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); if (htmlHelper.ViewContext.ClientValidationEnabled) { builder.GenerateId("form0"); } htmlHelper.ViewContext.HttpContext.Response.Write(builder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(htmlHelper.ViewContext); if (htmlHelper.ViewContext.ClientValidationEnabled) { // htmlHelper.ViewContext.FormContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return form; }
private static MvcForm FormHelper(HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary <string, object> htmlAttributes) { var builder = new TagBuilder("form"); builder.MergeAttributes <string, object>(htmlAttributes); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); if (htmlHelper.ViewContext.ClientValidationEnabled) { builder.GenerateId(((Func <string>)_formIdGeneratorPropertyInfo.GetValue(htmlHelper.ViewContext, null))()); } htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(htmlHelper.ViewContext); if (htmlHelper.ViewContext.ClientValidationEnabled) { htmlHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return(form); }
// When the object is created, write "begin" function public MainFormHelper(HtmlHelper htmlHelper) { object route; if (System.Web.HttpContext.Current.Request.QueryString["modal"] == "1" && System.Web.HttpContext.Current.Request.QueryString["closeonsave"] == "1") route = (object) new {modal = "1", closeonsave = "1"}; else route = System.Web.HttpContext.Current.Request.QueryString["modal"] == "1" ? (object) new {modal = "1"} : (object) new {}; _form = htmlHelper.BeginForm(null, null, route, FormMethod.Post, new { id = "mainForm" } ); var inputBuilder = new TagBuilder("input"); inputBuilder.Attributes.Add("type", "hidden"); inputBuilder.Attributes.Add("id", "UrlHash"); inputBuilder.Attributes.Add("name", "UrlHash"); var modelEdit = htmlHelper.ViewData.Model as ViewModelBase; inputBuilder.Attributes.Add("value", modelEdit != null ? modelEdit.UrlHash : ""); htmlHelper.ViewContext.Writer.Write(inputBuilder); }
public static MvcForm BeginForm(this HtmlHelper self, AppLocalUrl location, FormMethod method = FormMethod.Post, object htmlAttributes = null) { var viewContext = self.ViewContext; var actionUri = location.Resolved(viewContext.HttpContext); var tagBuilder = new TagBuilder("form"); if (htmlAttributes != null) { var attributeDictionary = (htmlAttributes as IDictionary <string, object>) ?? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); tagBuilder.MergeAttributes(attributeDictionary); } // action is implicitly generated, so htmlAttributes take precedence. tagBuilder.MergeAttribute("action", actionUri); // method is an explicit parameter, so it takes precedence over the htmlAttributes. tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); bool traditionalJavascriptEnabled = viewContext.ClientValidationEnabled && !viewContext.UnobtrusiveJavaScriptEnabled; if (traditionalJavascriptEnabled) { // forms must have an ID for client validation tagBuilder.GenerateId(GetFormIdGenerator(viewContext)); } viewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(viewContext); if (traditionalJavascriptEnabled) { viewContext.FormContext.FormId = tagBuilder.Attributes["id"]; } return(form); }
private static MvcForm FormHelper(this AjaxHelper ajaxHelper, string formAction, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) { TagBuilder builder = new TagBuilder("form"); builder.MergeAttributes(htmlAttributes); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", "post"); ajaxOptions = GetAjaxOptions(ajaxOptions); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } else { builder.MergeAttribute("onclick", FormOnClickValue); builder.MergeAttribute("onsubmit", GenerateAjaxScript(ajaxOptions, FormOnSubmitFormat)); } if (ajaxHelper.ViewContext.ClientValidationEnabled) { // forms must have an ID for client validation builder.GenerateId(ajaxHelper.ViewContext.FormIdGenerator()); } ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); MvcForm theForm = new MvcForm(ajaxHelper.ViewContext); if (ajaxHelper.ViewContext.ClientValidationEnabled) { ajaxHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return theForm; }
public ExampleConfigurator PostTo(string action, string controller) { this.form = this.htmlHelper.BeginForm(action, controller); return this; }
private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); // action is implicitly generated, so htmlAttributes take precedence. tagBuilder.MergeAttribute("action", formAction); // method is an explicit parameter, so it takes precedence over the htmlAttributes. tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); if (htmlHelper.ViewContext.ClientValidationEnabled) { // forms must have an ID for client validation tagBuilder.GenerateId(htmlHelper.ViewContext.FormIdGenerator()); } htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); MvcForm theForm = new MvcForm(htmlHelper.ViewContext); if (htmlHelper.ViewContext.ClientValidationEnabled) { htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"]; } return theForm; }
private static MvcForm FormHelper(HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) { var builder = new TagBuilder("form"); builder.MergeAttributes<string, object>(htmlAttributes); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); #if !MVC2 var flag = (htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled); #else var flag = htmlHelper.ViewContext.ClientValidationEnabled; #endif if (flag) builder.GenerateId(((Func<string>)_formIdGeneratorPropertyInfo.GetValue(htmlHelper.ViewContext, null))()); htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); var form = new MvcForm(htmlHelper.ViewContext); if (flag) htmlHelper.ViewContext.FormContext.FormId = builder.Attributes["id"]; return form; }
/// <summary> /// 输出AjaxForm表单 /// </summary> /// <param name="htmlHelper">被扩展的htmlHelper实例</param> /// <param name="formAction"></param> /// <param name="method">表单请求方式</param> /// <param name="options">异步提交表单选项</param> /// <param name="htmlAttributes">表单html属性集合</param> /// <returns>MvcForm</returns> private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, AjaxFormOptions options, IDictionary<string, object> htmlAttributes) { TagBuilder builder = new TagBuilder("form"); builder.MergeAttributes(htmlAttributes); if (!string.IsNullOrEmpty(formAction)) builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); builder.MergeAttributes(options.ToHtmlAttributes()); htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag) + htmlHelper.AntiForgeryToken()); MvcForm theForm = new MvcForm(htmlHelper.ViewContext); return theForm; }
private static MvcForm GenerateForm(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes, FormLayout layout) { var tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("action", formAction); tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); if (layout != FormLayout.Default) tagBuilder.AddCssClass("form-" + layout.ToString().ToLower()); htmlHelper.ViewContext.TempData["BootstrapFormLayout"] = layout; var flag = htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled; if (flag) tagBuilder.GenerateId(GenerateId(htmlHelper.ViewContext)); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); var mvcForm = new MvcForm(htmlHelper.ViewContext); if (flag) htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"]; return mvcForm; }
public AjaxContentValidation(ViewContext viewContext, MvcForm mvcForm) { _viewContext = viewContext; _mvcForm = mvcForm; }
public static MvcForm BeginForm(this HtmlHelper helper, string formAction, FormMethod method = FormMethod.Post, object attributes = null) { TagBuilder builder = new TagBuilder("form"); builder.MergeAttributes<string, object>(HtmlHelper.AnonymousObjectToHtmlAttributes(attributes)); builder.MergeAttribute("action", formAction); builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); bool flag = helper.ViewContext.ClientValidationEnabled && !helper.ViewContext.UnobtrusiveJavaScriptEnabled; if (flag) { builder.GenerateId(FormIdGenerator(helper)); } helper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag)); MvcForm form = new MvcForm(helper.ViewContext); if (flag) { helper.ViewContext.FormContext.FormId = builder.Attributes["id"]; } return form; }
/// <summary> /// /// </summary> /// <param name="viewContext"></param> /// <param name="form"></param> public AjaxContentValidation(ViewContext viewContext, MvcForm form) { this.viewContext = viewContext; this.form = form; }