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
This commit is contained in:
dougbu 2014-05-02 11:24:40 -07:00
parent ade41533f1
commit dd4fa762d5
1 changed files with 27 additions and 2 deletions

View File

@ -758,11 +758,36 @@ namespace Microsoft.AspNet.Mvc.Rendering
protected virtual MvcForm GenerateForm(string actionName, string controllerName, object routeValues,
FormMethod method, IDictionary<string, object> 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);