From dd4fa762d584d7bbb7fa59edd39a8781ef950197 Mon Sep 17 00:00:00 2001 From: dougbu Date: Fri, 2 May 2014 11:24:40 -0700 Subject: [PATCH] Change `BeginForm()` to add query string when called without parameters - use the request's Path and QueryString rather than default Action() return value - actual special case detects all parameter values match the defaults - this slightly expands the scenarios where the query string is added but removes an odd inconsistency between `html.BeginForm()` and (say) `html.BeginForm(FormMethod.Post)` Fixes #278 --- .../Rendering/Html/HtmlHelper.cs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs index 9f7dd88406..4d30164d31 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs @@ -758,11 +758,36 @@ namespace Microsoft.AspNet.Mvc.Rendering protected virtual MvcForm GenerateForm(string actionName, string controllerName, object routeValues, FormMethod method, IDictionary htmlAttributes) { - var formAction = _urlHelper.Action(action: actionName, controller: controllerName, values: routeValues); - var tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); + string formAction; + if (actionName == null && controllerName == null && routeValues == null && method == FormMethod.Post && + htmlAttributes == null) + { + // Submit to the original URL in the special case that user called the BeginForm() overload without + // parameters. Also reachable in the even-more-unusual case that user called another BeginForm() + // overload with default argument values. + var request = ViewContext.HttpContext.Request; + if (request.Path.HasValue) + { + formAction = request.Path.Value; + } + else + { + formAction = string.Empty; + } + + if (request.QueryString.HasValue) + { + formAction += request.QueryString.Value; + } + } + else + { + formAction = _urlHelper.Action(action: actionName, controller: controllerName, values: routeValues); + } + // action is implicitly generated, so htmlAttributes take precedence. tagBuilder.MergeAttribute("action", formAction);