[Fixes #3705] Bring back render partial

This commit is contained in:
javiercn 2015-12-15 12:20:06 -08:00
parent bbba9dcde6
commit c61cc65db3
2 changed files with 161 additions and 0 deletions

View File

@ -234,6 +234,94 @@ namespace Microsoft.AspNet.Mvc.Rendering
return result.GetAwaiter().GetResult();
}
/// <summary>
/// Renders HTML markup for the specified partial view.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <remarks>
/// In this context, "renders" means the method writes its output using <see cref="ViewContext.Writer"/>.
/// </remarks>
public static void RenderPartial(
this IHtmlHelper htmlHelper,
string partialViewName)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (partialViewName == null)
{
throw new ArgumentNullException(nameof(partialViewName));
}
var result = htmlHelper.RenderPartialAsync(partialViewName, htmlHelper.ViewData.Model, viewData: null);
result.GetAwaiter().GetResult();
}
/// <summary>
/// Renders HTML markup for the specified partial view.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <param name="viewData">A <see cref="ViewDataDictionary"/> to pass into the partial view.</param>
/// <remarks>
/// In this context, "renders" means the method writes its output using <see cref="ViewContext.Writer"/>.
/// </remarks>
public static void RenderPartial(
this IHtmlHelper htmlHelper,
string partialViewName,
ViewDataDictionary viewData)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (partialViewName == null)
{
throw new ArgumentNullException(nameof(partialViewName));
}
var result = htmlHelper.RenderPartialAsync(partialViewName, htmlHelper.ViewData.Model, viewData);
result.GetAwaiter().GetResult();
}
/// <summary>
/// Renders HTML markup for the specified partial view.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <param name="model">A model to pass into the partial view.</param>
/// <remarks>
/// In this context, "renders" means the method writes its output using <see cref="ViewContext.Writer"/>.
/// </remarks>
public static void RenderPartial(
this IHtmlHelper htmlHelper,
string partialViewName,
object model)
{
if (htmlHelper == null)
{
throw new ArgumentNullException(nameof(htmlHelper));
}
if (partialViewName == null)
{
throw new ArgumentNullException(nameof(partialViewName));
}
var result = htmlHelper.RenderPartialAsync(partialViewName, model, viewData: null);
result.GetAwaiter().GetResult();
}
/// <summary>
/// Renders HTML markup for the specified partial view.
/// </summary>

View File

@ -140,6 +140,79 @@ namespace Microsoft.AspNet.Mvc.Rendering
helper.VerifyAll();
}
// Func<IHtmlHelper, IHtmlContent>, expected Model, expected ViewDataDictionary
public static TheoryData<Action<IHtmlHelper>, object, ViewDataDictionary> RenderPartialExtensionMethods
{
get
{
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var model = new object();
return new TheoryData<Action<IHtmlHelper>, object, ViewDataDictionary>
{
{ helper => helper.RenderPartial("test"), null, null },
{ helper => helper.RenderPartial("test", model), model, null },
{ helper => helper.RenderPartial("test", viewData), null, viewData },
};
}
}
[Theory]
[MemberData(nameof(RenderPartialExtensionMethods))]
public void RenderPartialMethods_DoesNotWrapThrownException(
Action<IHtmlHelper> partialMethod,
object unusedModel,
ViewDataDictionary unusedViewData)
{
// Arrange
var expected = new InvalidOperationException();
var helper = new Mock<IHtmlHelper>();
helper.Setup(h => h.RenderPartialAsync("test", It.IsAny<object>(), It.IsAny<ViewDataDictionary>()))
.Callback(() =>
{
// Workaround for compilation issue with Moq.
helper.ToString();
throw expected;
});
helper.SetupGet(h => h.ViewData)
.Returns(new ViewDataDictionary(new EmptyModelMetadataProvider()));
// Act and Assert
var actual = Assert.Throws<InvalidOperationException>(() => partialMethod(helper.Object));
Assert.Same(expected, actual);
}
[Theory]
[MemberData(nameof(RenderPartialAsyncExtensionMethods))]
public async Task RenderPartialMethods_CallHtmlHelperWithExpectedArguments(
Func<IHtmlHelper, Task> renderPartialAsyncMethod,
object expectedModel,
ViewDataDictionary expectedViewData)
{
// Arrange
var htmlContent = Mock.Of<IHtmlContent>();
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
if (expectedModel == null)
{
// Extension methods without model parameter use ViewData.Model to get Model.
var viewData = expectedViewData ?? new ViewDataDictionary(new EmptyModelMetadataProvider());
helper
.SetupGet(h => h.ViewData)
.Returns(viewData)
.Verifiable();
}
helper
.Setup(h => h.RenderPartialAsync("test", expectedModel, expectedViewData))
.Returns(Task.FromResult(true))
.Verifiable();
// Act
await renderPartialAsyncMethod(helper.Object);
// Assert
helper.VerifyAll();
}
// Func<IHtmlHelper, IHtmlContent>, expected Model, expected ViewDataDictionary
public static TheoryData<Func<IHtmlHelper, Task>, object, ViewDataDictionary> RenderPartialAsyncExtensionMethods
{