From e51a118a9de70ebe17b3904e15c30ff3236ac267 Mon Sep 17 00:00:00 2001 From: Crystal Qian Date: Tue, 28 Jun 2016 09:38:37 -0700 Subject: [PATCH] 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 --- .../PartialViewResult.cs | 5 ++++ .../ViewComponentResult.cs | 25 +++++++++++-------- .../ControllerUnitTestabilityTests.cs | 14 ++--------- .../Internal/PartialViewResultTest.cs | 17 +++++++++++++ .../ViewComponentResultTest.cs | 17 +++++++++++++ 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs index 7abf63632e..1f50b436a6 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/PartialViewResult.cs @@ -29,6 +29,11 @@ namespace Microsoft.AspNetCore.Mvc /// public string ViewName { get; set; } + /// + /// Gets the view data model. + /// + public object Model => ViewData?.Model; + /// /// Gets or sets the used for rendering the view for this result. /// diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs index fce647624e..943745627c 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponentResult.cs @@ -20,21 +20,11 @@ namespace Microsoft.AspNetCore.Mvc /// public object Arguments { get; set; } - /// - /// Gets or sets the Content-Type header for the response. - /// - public string ContentType { get; set; } - /// /// Gets or sets the HTTP status code. /// public int? StatusCode { get; set; } - /// - /// Gets or sets the for this result. - /// - public ITempDataDictionary TempData { get; set; } - /// /// Gets or sets the name of the view component to invoke. Will be ignored if /// is set to a non-null value. @@ -46,11 +36,21 @@ namespace Microsoft.AspNetCore.Mvc /// public Type ViewComponentType { get; set; } + /// + /// Get the view data model. + /// + public object Model => ViewData?.Model; + /// /// Gets or sets the for this result. /// public ViewDataDictionary ViewData { get; set; } + /// + /// Gets or sets the for this result. + /// + public ITempDataDictionary TempData { get; set; } + /// /// Gets or sets the used to locate views. /// @@ -58,6 +58,11 @@ namespace Microsoft.AspNetCore.Mvc /// ActionContext.HttpContext.RequestServices is used. public IViewEngine ViewEngine { get; set; } + /// + /// Gets or sets the Content-Type header for the response. + /// + public string ContentType { get; set; } + /// public override Task ExecuteResultAsync(ActionContext context) { diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ControllerUnitTestabilityTests.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ControllerUnitTestabilityTests.cs index 3eb6c5cc40..95ee71bedb 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ControllerUnitTestabilityTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ControllerUnitTestabilityTests.cs @@ -34,15 +34,10 @@ namespace Microsoft.AspNetCore.Mvc var viewResult = Assert.IsType(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(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] diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs index cf5adf0247..7b82f63066 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs @@ -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() { diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentResultTest.cs index 6985134f7c..728e71daec 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentResultTest.cs @@ -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() {