public MvcForm LayuiBeginForm(string actionName, string controllerName, object routeValues, FormMethod method, bool?antiforgery, object htmlAttributes) { ViewContext.FormContext = new FormContext { CanRenderAtEndOfForm = true }; var tagBuilder = _htmlGenerator.GenerateForm(ViewContext, actionName, controllerName, routeValues, HtmlHelper.GetFormMethodString(method), htmlAttributes); if (tagBuilder != null) { tagBuilder.AddCssClass("layui-form"); tagBuilder.MergeAttribute("auto-bind", "true", replaceExisting: false); tagBuilder.TagRenderMode = TagRenderMode.StartTag; tagBuilder.WriteTo(ViewContext.Writer, _htmlEncoder); } var shouldGenerateAntiforgery = antiforgery ?? method != FormMethod.Get; if (shouldGenerateAntiforgery) { ViewContext.FormContext.EndOfFormContent.Add(_htmlGenerator.GenerateAntiforgery(ViewContext)); } return(new MvcForm(ViewContext, _htmlEncoder)); }
/// <summary> /// Renders a <form> start tag to the response. When the user submits the form, the action with name /// <paramref name="actionName"/> will process the request. /// </summary> /// <param name="actionName">The name of the action method.</param> /// <param name="controllerName">The name of the controller.</param> /// <param name="routeValues"> /// An <see cref="object"/> that contains the parameters for a route. The parameters are retrieved through /// reflection by examining the properties of the <see cref="object"/>. This <see cref="object"/> is typically /// created using <see cref="object"/> initializer syntax. Alternatively, an /// <see cref="IDictionary{String, Object}"/> instance containing the route parameters. /// </param> /// <param name="method">The HTTP method for processing the form, either GET or POST.</param> /// <param name="antiforgery"> /// If <c>true</c>, <form> elements will include an antiforgery token. /// If <c>false</c>, suppresses the generation an <input> of type "hidden" with an antiforgery token. /// If <c>null</c>, <form> elements will include an antiforgery token only if /// <paramref name="method"/> is not <see cref="FormMethod.Get"/>. /// </param> /// <param name="htmlAttributes"> /// An <see cref="object"/> that contains the HTML attributes for the element. Alternatively, an /// <see cref="IDictionary{String, Object}"/> instance containing the HTML attributes. /// </param> /// <returns> /// An <see cref="MvcForm"/> instance which renders the </form> end tag when disposed. /// </returns> /// <remarks> /// In this context, "renders" means the method writes its output using <see cref="ViewContext.Writer"/>. /// </remarks> protected virtual MvcForm GenerateForm( string actionName, string controllerName, object routeValues, FormMethod method, bool?antiforgery, object htmlAttributes) { var tagBuilder = _htmlGenerator.GenerateForm( ViewContext, actionName, controllerName, routeValues, GetFormMethodString(method), htmlAttributes); if (tagBuilder != null) { tagBuilder.TagRenderMode = TagRenderMode.StartTag; tagBuilder.WriteTo(ViewContext.Writer, _htmlEncoder); } var shouldGenerateAntiforgery = antiforgery.HasValue ? antiforgery.Value : method != FormMethod.Get; if (shouldGenerateAntiforgery) { ViewContext.FormContext.EndOfFormContent.Add(_htmlGenerator.GenerateAntiforgery(ViewContext)); ViewContext.FormContext.HasAntiforgeryToken = true; } return(CreateForm()); }
/// <summary> /// Renders a <form> start tag to the response. When the user submits the form, the action with name /// <paramref name="actionName"/> will process the request. /// </summary> /// <param name="actionName">The name of the action method.</param> /// <param name="controllerName">The name of the controller.</param> /// <param name="routeValues"> /// An <see cref="object"/> that contains the parameters for a route. The parameters are retrieved through /// reflection by examining the properties of the <see cref="object"/>. This <see cref="object"/> is typically /// created using <see cref="object"/> initializer syntax. Alternatively, an /// <see cref="IDictionary{string, object}"/> instance containing the route parameters. /// </param> /// <param name="method">The HTTP method for processing the form, either GET or POST.</param> /// <param name="htmlAttributes"> /// An <see cref="object"/> that contains the HTML attributes for the element. Alternatively, an /// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes. /// </param> /// <returns> /// An <see cref="MvcForm"/> instance which renders the </form> end tag when disposed. /// </returns> /// <remarks> /// In this context, "renders" means the method writes its output using <see cref="ViewContext.Writer"/>. /// </remarks> protected virtual MvcForm GenerateForm( string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes) { var tagBuilder = _htmlGenerator.GenerateForm( ViewContext, actionName, controllerName, routeValues, GetFormMethodString(method), htmlAttributes); if (tagBuilder != null) { tagBuilder.TagRenderMode = TagRenderMode.StartTag; // As a perf optimization, we can buffer this output rather than writing it // out character by character. var htmlWriter = ViewContext.Writer as HtmlTextWriter; if (htmlWriter == null) { tagBuilder.WriteTo(ViewContext.Writer, _htmlEncoder); } else { htmlWriter.Write(tagBuilder); } } return(CreateForm()); }
public override void Process(TagHelperContext context, TagHelperOutput output) { context.ThrowIfNull(); output.ThrowIfNull(); output.TagName = "form"; output.TagMode = TagMode.StartTagAndEndTag; var htmlAttributes = new Dictionary <string, object> { { "class", "layui-form layuiadmin-card-header-auto layui-form-pane" }, { "id", Id }, { "lay-filter", Id } }; var tagBuilder = _generator.GenerateForm(ViewContext, Action, Controller, null, "post", htmlAttributes); output.MergeAttributes(tagBuilder); output.PostContent.SetHtmlContent( "<div class='layui-form-item layui-hide'><input type='button' lay-submit lay-filter='btn-submit' id='btn-submit' value='确认'> </div>"); output.PostElement.SetHtmlContent($@"<script> $(function () {{ layui.form.on('submit(btn-submit)', function () {{ $('#{Id}').formPost(function () {{ var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); }}); }}); }}); </script>"); }
public override void Process(TagHelperContext context, TagHelperOutput output) { TagBuilder tagBuilder = Generator.GenerateForm(ViewContext, ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), null, null, null); output.MergeAttributes(tagBuilder); if (Antiforgery == true) { output.PostContent.AppendHtml(Generator.GenerateAntiforgery(ViewContext)); } }
/// <summary> /// Generates a form. /// </summary> /// <param name="generator">The <see cref="IHtmlGenerator"/>.</param> /// <param name="viewContext">The <see cref="ViewContext"/>.</param> /// <param name="actionName">The action name.</param> /// <param name="controllerName">The name of the controller.</param> /// <param name="fragment">The fragment.</param> /// <param name="routeValues">The route values.</param> /// <param name="method">The form method.</param> /// <param name="htmlAttributes">The html attributes.</param> /// <returns></returns> public static TagBuilder GenerateForm( this IHtmlGenerator generator, ViewContext viewContext, string actionName, string controllerName, string fragment, object routeValues, string method, object htmlAttributes) { var tagBuilder = generator.GenerateForm(viewContext, actionName, controllerName, routeValues, method, htmlAttributes); // Append the fragment to action if (fragment != null) { tagBuilder.Attributes["action"] += "#" + fragment; } return(tagBuilder); }
/// <summary> /// Renders a <form> start tag to the response. When the user submits the form, the action with name /// <paramref name="actionName"/> will process the request. /// </summary> /// <param name="actionName">The name of the action method.</param> /// <param name="controllerName">The name of the controller.</param> /// <param name="routeValues"> /// An <see cref="object"/> that contains the parameters for a route. The parameters are retrieved through /// reflection by examining the properties of the <see cref="object"/>. This <see cref="object"/> is typically /// created using <see cref="object"/> initializer syntax. Alternatively, an /// <see cref="IDictionary{string, object}"/> instance containing the route parameters. /// </param> /// <param name="method">The HTTP method for processing the form, either GET or POST.</param> /// <param name="htmlAttributes"> /// An <see cref="object"/> that contains the HTML attributes for the element. Alternatively, an /// <see cref="IDictionary{string, object}"/> instance containing the HTML attributes. /// </param> /// <returns> /// An <see cref="MvcForm"/> instance which renders the </form> end tag when disposed. /// </returns> /// <remarks> /// In this context, "renders" means the method writes its output using <see cref="ViewContext.Writer"/>. /// </remarks> protected virtual MvcForm GenerateForm( string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes) { var tagBuilder = _htmlGenerator.GenerateForm( ViewContext, actionName, controllerName, routeValues, GetFormMethodString(method), htmlAttributes); if (tagBuilder != null) { ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); } return(CreateForm()); }
public override void Process(TagHelperContext context, TagHelperOutput output) { context.ThrowIfNull(); output.ThrowIfNull(); output.TagName = "form"; output.TagMode = TagMode.StartTagAndEndTag; var htmlAttributes = new Dictionary <string, object> { { "class", "layui-form layui-form-pane" }, { "id", Id } }; var tagBuilder = _generator.GenerateForm(ViewContext, Action, Controller, null, "post", htmlAttributes); output.MergeAttributes(tagBuilder); output.PostElement.SetHtmlContent(@"<script type='text/javascript'> layui.use('form', function () {{ var form = layui.form; form.render(); }}); </script>"); }
public TagBuilder GenerateForm(ViewContext viewContext, string actionName, string controllerName, object routeValues, string method, object htmlAttributes) { return(_htmlGenerator.GenerateForm(viewContext, actionName, controllerName, routeValues, method, htmlAttributes)); }