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:
parent
7f68a47fde
commit
9eb87d4676
|
|
@ -182,12 +182,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
}
|
}
|
||||||
|
|
||||||
string action;
|
string action;
|
||||||
if (actionName == null && controllerName == null && routeValues == null && defaultMethod &&
|
if (actionName == null && controllerName == null && routeValues == null && defaultMethod)
|
||||||
htmlAttributes == null)
|
|
||||||
{
|
{
|
||||||
// Submit to the original URL in the special case that user called the BeginForm() overload without
|
// 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()
|
// parameters (except for the htmlAttributes parameter). Also reachable in the even-more-unusual case
|
||||||
// overload with default argument values.
|
// that user called another BeginForm() overload with default argument values.
|
||||||
var request = viewContext.HttpContext.Request;
|
var request = viewContext.HttpContext.Request;
|
||||||
action = request.PathBase + request.Path + request.QueryString;
|
action = request.PathBase + request.Path + request.QueryString;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -158,6 +158,55 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
Assert.Equal("</form>", builder.ToString());
|
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]
|
[Theory]
|
||||||
[MemberData(nameof(BeginFormDataSet))]
|
[MemberData(nameof(BeginFormDataSet))]
|
||||||
public void BeginForm_RendersExpectedValues(
|
public void BeginForm_RendersExpectedValues(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue