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:
parent
c319ae51a5
commit
e51a118a9d
|
|
@ -29,6 +29,11 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public string ViewName { get; set; }
|
public string ViewName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the view data model.
|
||||||
|
/// </summary>
|
||||||
|
public object Model => ViewData?.Model;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the <see cref="ViewDataDictionary"/> used for rendering the view for this result.
|
/// Gets or sets the <see cref="ViewDataDictionary"/> used for rendering the view for this result.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -20,21 +20,11 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public object Arguments { get; set; }
|
public object Arguments { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the Content-Type header for the response.
|
|
||||||
/// </summary>
|
|
||||||
public string ContentType { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the HTTP status code.
|
/// Gets or sets the HTTP status code.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int? StatusCode { get; set; }
|
public int? StatusCode { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the <see cref="ITempDataDictionary"/> for this result.
|
|
||||||
/// </summary>
|
|
||||||
public ITempDataDictionary TempData { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the name of the view component to invoke. Will be ignored if <see cref="ViewComponentType"/>
|
/// 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.
|
/// is set to a non-<c>null</c> value.
|
||||||
|
|
@ -46,11 +36,21 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Type ViewComponentType { get; set; }
|
public Type ViewComponentType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the view data model.
|
||||||
|
/// </summary>
|
||||||
|
public object Model => ViewData?.Model;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the <see cref="ViewDataDictionary"/> for this result.
|
/// Gets or sets the <see cref="ViewDataDictionary"/> for this result.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ViewDataDictionary ViewData { get; set; }
|
public ViewDataDictionary ViewData { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="ITempDataDictionary"/> for this result.
|
||||||
|
/// </summary>
|
||||||
|
public ITempDataDictionary TempData { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the <see cref="IViewEngine"/> used to locate views.
|
/// Gets or sets the <see cref="IViewEngine"/> used to locate views.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -58,6 +58,11 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// <c>ActionContext.HttpContext.RequestServices</c> is used.</remarks>
|
/// <c>ActionContext.HttpContext.RequestServices</c> is used.</remarks>
|
||||||
public IViewEngine ViewEngine { get; set; }
|
public IViewEngine ViewEngine { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the Content-Type header for the response.
|
||||||
|
/// </summary>
|
||||||
|
public string ContentType { get; set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override Task ExecuteResultAsync(ActionContext context)
|
public override Task ExecuteResultAsync(ActionContext context)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -34,15 +34,10 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
var viewResult = Assert.IsType<ViewResult>(result);
|
var viewResult = Assert.IsType<ViewResult>(result);
|
||||||
Assert.Equal(viewName, viewResult.ViewName);
|
Assert.Equal(viewName, viewResult.ViewName);
|
||||||
Assert.NotNull(viewResult.ViewData);
|
Assert.NotNull(viewResult.ViewData);
|
||||||
|
Assert.Same(model, viewResult.Model);
|
||||||
Assert.Same(model, viewResult.ViewData.Model);
|
Assert.Same(model, viewResult.ViewData.Model);
|
||||||
Assert.Same(controller.ViewData, viewResult.ViewData);
|
Assert.Same(controller.ViewData, viewResult.ViewData);
|
||||||
Assert.Same(controller.TempData, viewResult.TempData);
|
Assert.Same(controller.TempData, viewResult.TempData);
|
||||||
|
|
||||||
if (model != null)
|
|
||||||
{
|
|
||||||
Assert.IsType(model.GetType(), viewResult.ViewData.Model);
|
|
||||||
Assert.NotNull(viewResult.ViewData.Model);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -61,15 +56,10 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
var viewResult = Assert.IsType<PartialViewResult>(result);
|
var viewResult = Assert.IsType<PartialViewResult>(result);
|
||||||
Assert.Equal(viewName, viewResult.ViewName);
|
Assert.Equal(viewName, viewResult.ViewName);
|
||||||
Assert.NotNull(viewResult.ViewData);
|
Assert.NotNull(viewResult.ViewData);
|
||||||
|
Assert.Same(model, viewResult.Model);
|
||||||
Assert.Same(model, viewResult.ViewData.Model);
|
Assert.Same(model, viewResult.ViewData.Model);
|
||||||
Assert.Same(controller.ViewData, viewResult.ViewData);
|
Assert.Same(controller.ViewData, viewResult.ViewData);
|
||||||
Assert.Same(controller.TempData, viewResult.TempData);
|
Assert.Same(controller.TempData, viewResult.TempData);
|
||||||
|
|
||||||
if (model != null)
|
|
||||||
{
|
|
||||||
Assert.IsType(model.GetType(), viewResult.ViewData.Model);
|
|
||||||
Assert.NotNull(viewResult.ViewData.Model);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,23 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
|
||||||
// and ViewExecutorTest for more comprehensive tests.
|
// and ViewExecutorTest for more comprehensive tests.
|
||||||
public class PartialViewResultTest
|
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]
|
[Fact]
|
||||||
public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesGetViewLocations()
|
public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesGetViewLocations()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,23 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
private readonly ITempDataDictionary _tempDataDictionary =
|
private readonly ITempDataDictionary _tempDataDictionary =
|
||||||
new TempDataDictionary(new DefaultHttpContext(), new SessionStateTempDataProvider());
|
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]
|
[Fact]
|
||||||
public async Task ExecuteAsync_ViewComponentResult_AllowsNullViewDataAndTempData()
|
public async Task ExecuteAsync_ViewComponentResult_AllowsNullViewDataAndTempData()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue