Removed htmlAttributes null check in GenerateForm

The null check caused the form not to use the default url generation
behavior when html attributes were specified.
This commit is contained in:
Henk Mollema 2015-04-13 15:24:39 +02:00
parent 7f68a47fde
commit 9eb87d4676
2 changed files with 52 additions and 4 deletions

View File

@ -182,12 +182,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
}
string action;
if (actionName == null && controllerName == null && routeValues == null && defaultMethod &&
htmlAttributes == null)
if (actionName == null && controllerName == null && routeValues == null && defaultMethod)
{
// 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.
// parameters (except for the htmlAttributes parameter). Also reachable in the even-more-unusual case
// that user called another BeginForm() overload with default argument values.
var request = viewContext.HttpContext.Request;
action = request.PathBase + request.Path + request.QueryString;
}

View File

@ -158,6 +158,55 @@ namespace Microsoft.AspNet.Mvc.Rendering
Assert.Equal("</form>", builder.ToString());
}
[Fact]
public void BeginForm_RendersExpectedValues_WithDefaultArgumentsAndHtmlAttributes()
{
// Arrange
var pathBase = "/Base";
var path = "/Path";
var queryString = "?query=string";
var expectedAction = pathBase + path + queryString;
var htmlAttributes = new { p1_name = "p1-value" };
var expectedStartTag = string.Format("<form action=\"{0}\" method=\"post\"{1}>",
expectedAction,
GetHtmlAttributesAsString(htmlAttributes));
// IUrlHelper should not be used in this scenario.
var urlHelper = new Mock<IUrlHelper>(MockBehavior.Strict);
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(urlHelper.Object);
// Guards
Assert.NotNull(htmlHelper.ViewContext);
var writer = Assert.IsAssignableFrom<StringWriter>(htmlHelper.ViewContext.Writer);
var builder = writer.GetStringBuilder();
Assert.NotNull(builder);
Assert.NotNull(htmlHelper.ViewContext.HttpContext);
var request = htmlHelper.ViewContext.HttpContext.Request;
Assert.NotNull(request);
// Set properties the IHtmlGenerator implementation should use in this scenario.
request.PathBase = new PathString(pathBase);
request.Path = new PathString(path);
request.QueryString = new QueryString(queryString);
// Act
var mvcForm = htmlHelper.BeginForm(
actionName: null,
controllerName: null,
routeValues: null,
method: FormMethod.Post,
htmlAttributes: htmlAttributes);
// Assert
Assert.NotNull(mvcForm);
Assert.Equal(expectedStartTag, builder.ToString());
urlHelper.Verify();
builder.Clear();
mvcForm.Dispose();
Assert.Equal("</form>", builder.ToString());
}
[Theory]
[MemberData(nameof(BeginFormDataSet))]
public void BeginForm_RendersExpectedValues(