Added consistent model property to view result variants (#4901)

* Added consistent model property/tests to ViewResult, PartialViewResult, ViewComponentResult. This resolves #4813.

* Removed unnecessary model asserts

* Removed redundant model checking
This commit is contained in:
Crystal Qian 2016-06-28 09:38:37 -07:00 committed by GitHub
parent c319ae51a5
commit e51a118a9d
5 changed files with 56 additions and 22 deletions

View File

@ -29,6 +29,11 @@ namespace Microsoft.AspNetCore.Mvc
/// </remarks>
public string ViewName { get; set; }
/// <summary>
/// Gets the view data model.
/// </summary>
public object Model => ViewData?.Model;
/// <summary>
/// Gets or sets the <see cref="ViewDataDictionary"/> used for rendering the view for this result.
/// </summary>

View File

@ -20,21 +20,11 @@ namespace Microsoft.AspNetCore.Mvc
/// </summary>
public object Arguments { get; set; }
/// <summary>
/// Gets or sets the Content-Type header for the response.
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// Gets or sets the HTTP status code.
/// </summary>
public int? StatusCode { get; set; }
/// <summary>
/// Gets or sets the <see cref="ITempDataDictionary"/> for this result.
/// </summary>
public ITempDataDictionary TempData { get; set; }
/// <summary>
/// Gets or sets the name of the view component to invoke. Will be ignored if <see cref="ViewComponentType"/>
/// is set to a non-<c>null</c> value.
@ -46,11 +36,21 @@ namespace Microsoft.AspNetCore.Mvc
/// </summary>
public Type ViewComponentType { get; set; }
/// <summary>
/// Get the view data model.
/// </summary>
public object Model => ViewData?.Model;
/// <summary>
/// Gets or sets the <see cref="ViewDataDictionary"/> for this result.
/// </summary>
public ViewDataDictionary ViewData { get; set; }
/// <summary>
/// Gets or sets the <see cref="ITempDataDictionary"/> for this result.
/// </summary>
public ITempDataDictionary TempData { get; set; }
/// <summary>
/// Gets or sets the <see cref="IViewEngine"/> used to locate views.
/// </summary>
@ -58,6 +58,11 @@ namespace Microsoft.AspNetCore.Mvc
/// <c>ActionContext.HttpContext.RequestServices</c> is used.</remarks>
public IViewEngine ViewEngine { get; set; }
/// <summary>
/// Gets or sets the Content-Type header for the response.
/// </summary>
public string ContentType { get; set; }
/// <inheritdoc />
public override Task ExecuteResultAsync(ActionContext context)
{

View File

@ -34,15 +34,10 @@ namespace Microsoft.AspNetCore.Mvc
var viewResult = Assert.IsType<ViewResult>(result);
Assert.Equal(viewName, viewResult.ViewName);
Assert.NotNull(viewResult.ViewData);
Assert.Same(model, viewResult.Model);
Assert.Same(model, viewResult.ViewData.Model);
Assert.Same(controller.ViewData, viewResult.ViewData);
Assert.Same(controller.TempData, viewResult.TempData);
if (model != null)
{
Assert.IsType(model.GetType(), viewResult.ViewData.Model);
Assert.NotNull(viewResult.ViewData.Model);
}
}
[Theory]
@ -61,15 +56,10 @@ namespace Microsoft.AspNetCore.Mvc
var viewResult = Assert.IsType<PartialViewResult>(result);
Assert.Equal(viewName, viewResult.ViewName);
Assert.NotNull(viewResult.ViewData);
Assert.Same(model, viewResult.Model);
Assert.Same(model, viewResult.ViewData.Model);
Assert.Same(controller.ViewData, viewResult.ViewData);
Assert.Same(controller.TempData, viewResult.TempData);
if (model != null)
{
Assert.IsType(model.GetType(), viewResult.ViewData.Model);
Assert.NotNull(viewResult.ViewData.Model);
}
}
[Fact]

View File

@ -22,6 +22,23 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
// and ViewExecutorTest for more comprehensive tests.
public class PartialViewResultTest
{
[Fact]
public void Model_ExposesViewDataModel()
{
// Arrange
var customModel = new object();
var viewResult = new PartialViewResult
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
{
Model = customModel
},
};
// Act & Assert
Assert.Same(customModel, viewResult.Model);
}
[Fact]
public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesGetViewLocations()
{

View File

@ -34,6 +34,23 @@ namespace Microsoft.AspNetCore.Mvc
private readonly ITempDataDictionary _tempDataDictionary =
new TempDataDictionary(new DefaultHttpContext(), new SessionStateTempDataProvider());
[Fact]
public void Model_ExposesViewDataModel()
{
// Arrange
var customModel = new object();
var viewResult = new ViewComponentResult
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
{
Model = customModel
},
};
// Act & Assert
Assert.Same(customModel, viewResult.Model);
}
[Fact]
public async Task ExecuteAsync_ViewComponentResult_AllowsNullViewDataAndTempData()
{