Add Html.Partial - sync versions of Html.PartialAsync

Fixes #1107
This commit is contained in:
Pranav K 2014-10-14 07:28:35 -07:00
parent d9ebb37906
commit a41b9dc983
4 changed files with 297 additions and 6 deletions

View File

@ -68,6 +68,98 @@ namespace Microsoft.AspNet.Mvc.Rendering
return htmlHelper.PartialAsync(partialViewName, model, viewData: null);
}
/// <summary>
/// Returns 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>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName)
{
return Partial(htmlHelper, partialViewName, htmlHelper.ViewData.Model, viewData: null);
}
/// <summary>
/// Returns 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>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName,
ViewDataDictionary viewData)
{
return Partial(htmlHelper, partialViewName, htmlHelper.ViewData.Model, viewData);
}
/// <summary>
/// Returns 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>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName,
object model)
{
return Partial(htmlHelper, partialViewName, model, viewData: null);
}
/// <summary>
/// Returns HTML markup for the specified partial view.
/// </summary>
/// <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>
/// <param name="viewData">A <see cref="ViewDataDictionary"/> to pass into the partial view.</param>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName,
object model,
ViewDataDictionary viewData)
{
var result = htmlHelper.PartialAsync(partialViewName, model, viewData);
return TaskHelper.WaitAndThrowIfFaulted(result);
}
/// <summary>
/// Renders HTML markup for the specified partial view.
/// </summary>

View File

@ -6,17 +6,15 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Security.DataProtection;
using Microsoft.Framework.OptionsModel;
using Moq;
namespace Microsoft.AspNet.Mvc.Core
namespace Microsoft.AspNet.Mvc.Rendering
{
public class DefaultTemplatesUtilities
{
@ -143,6 +141,12 @@ namespace Microsoft.AspNet.Mvc.Core
return htmlHelper;
}
public static string FormatOutput(IHtmlHelper helper, object model)
{
var metadata = helper.MetadataProvider.GetMetadataForType(() => model, model.GetType());
return FormatOutput(metadata);
}
private static ICompositeViewEngine CreateViewEngine()
{
var view = new Mock<IView>();

View File

@ -5,11 +5,11 @@ using System.Globalization;
using Microsoft.AspNet.Mvc.ModelBinding;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core
namespace Microsoft.AspNet.Mvc.Rendering
{
/// <summary>
/// Test the <see cref="Rendering.IHtmlHelper.DisplayText"/> and
/// <see cref="Rendering.IHtmlHelper{TModel}.DisplayTextFor{TValue}"/> methods.
/// Test the <see cref="IHtmlHelper.DisplayText"/> and
/// <see cref="IHtmlHelper{TModel}.DisplayTextFor{TValue}"/> methods.
/// </summary>
public class HtmlHelperDisplayTextTest
{

View File

@ -0,0 +1,195 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.ModelBinding;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Rendering
{
public class HtmlHelperPartialExtensionsTest
{
public static TheoryData<Func<IHtmlHelper, HtmlString>> PartialExtensionMethods
{
get
{
var vdd = new ViewDataDictionary(new EmptyModelMetadataProvider());
return new TheoryData<Func<IHtmlHelper, HtmlString>>
{
helper => helper.Partial("test"),
helper => helper.Partial("test", new object()),
helper => helper.Partial("test", vdd),
helper => helper.Partial("test", new object(), vdd)
};
}
}
[Theory]
[MemberData(nameof(PartialExtensionMethods))]
public void PartialMethods_DoesNotWrapThrownException(Func<IHtmlHelper, HtmlString> partialMethod)
{
// Arrange
var expected = new InvalidOperationException();
var helper = new Mock<IHtmlHelper>();
helper.Setup(h => h.PartialAsync("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);
}
[Fact]
public void Partial_InvokesPartialAsyncWithCurrentModel()
{
// Arrange
var expected = new HtmlString("value");
var model = new object();
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
{
Model = model
};
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", model, null))
.Returns(Task.FromResult(expected))
.Verifiable();
helper.SetupGet(h => h.ViewData)
.Returns(viewData);
// Act
var actual = helper.Object.Partial("test");
// Assert
Assert.Same(expected, actual);
helper.Verify();
}
[Fact]
public void PartialWithModel_InvokesPartialAsyncWithPassedInModel()
{
// Arrange
var expected = new HtmlString("value");
var model = new object();
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", model, null))
.Returns(Task.FromResult(expected))
.Verifiable();
// Act
var actual = helper.Object.Partial("test", model);
// Assert
Assert.Same(expected, actual);
helper.Verify();
}
[Fact]
public void PartialWithViewData_InvokesPartialAsyncWithPassedInViewData()
{
// Arrange
var expected = new HtmlString("value");
var model = new object();
var passedInViewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
{
Model = model
};
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", model, passedInViewData))
.Returns(Task.FromResult(expected))
.Verifiable();
helper.SetupGet(h => h.ViewData)
.Returns(viewData);
// Act
var actual = helper.Object.Partial("test", passedInViewData);
// Assert
Assert.Same(expected, actual);
helper.Verify();
}
[Fact]
public void PartialWithViewDataAndModel_InvokesPartialAsyncWithPassedInViewDataAndModel()
{
// Arrange
var expected = new HtmlString("value");
var passedInModel = new object();
var passedInViewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", passedInModel, passedInViewData))
.Returns(Task.FromResult(expected))
.Verifiable();
// Act
var actual = helper.Object.Partial("test", passedInModel, passedInViewData);
// Assert
Assert.Same(expected, actual);
helper.Verify();
}
[Fact]
public void Partial_InvokesAndRendersPartialAsyncOnHtmlHelperOfT()
{
// Arrange
var model = new TestModel();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
var expected = DefaultTemplatesUtilities.FormatOutput(helper, model);
// Act
var actual = helper.Partial("some-partial");
// Assert
Assert.Equal(expected, actual.ToString());
}
[Fact]
public void PartialWithModel_InvokesAndRendersPartialAsyncOnHtmlHelperOfT()
{
// Arrange
var model = new TestModel();
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
var expected = DefaultTemplatesUtilities.FormatOutput(helper, model);
// Act
var actual = helper.Partial("some-partial", model);
// Assert
Assert.Equal(expected, actual.ToString());
}
[Fact]
public void PartialWithViewData_InvokesAndRendersPartialAsyncOnHtmlHelperOfT()
{
// Arrange
var model = new TestModel();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
var viewData = new ViewDataDictionary(helper.MetadataProvider);
var expected = DefaultTemplatesUtilities.FormatOutput(helper, model);
// Act
var actual = helper.Partial("some-partial", viewData);
// Assert
Assert.Equal(expected, actual.ToString());
}
private sealed class TestModel
{
public override string ToString()
{
return "test-model-content";
}
}
}
}