Change `ViewComponent.View()` to flow the `ViewData.Model`.

- This is more consistent with how controllers work.

#4882
This commit is contained in:
N. Taylor Mullen 2016-08-18 15:19:37 -07:00
parent c942eab6e2
commit 7036e2b0f5
3 changed files with 25 additions and 3 deletions

View File

@ -138,7 +138,6 @@ namespace Microsoft.AspNetCore.Mvc
[NonAction]
public virtual ViewResult View(string viewName, object model)
{
// Do not override ViewData.Model unless passed a non-null value.
if (model != null)
{
ViewData.Model = model;
@ -195,7 +194,6 @@ namespace Microsoft.AspNetCore.Mvc
[NonAction]
public virtual PartialViewResult PartialView(string viewName, object model)
{
// Do not override ViewData.Model unless passed a non-null value.
if (model != null)
{
ViewData.Model = model;

View File

@ -262,7 +262,12 @@ namespace Microsoft.AspNetCore.Mvc
/// <returns>A <see cref="ViewViewComponentResult"/>.</returns>
public ViewViewComponentResult View<TModel>(string viewName, TModel model)
{
var viewData = new ViewDataDictionary<TModel>(ViewData, model);
if (model != null)
{
ViewData.Model = model;
}
var viewData = new ViewDataDictionary<TModel>(ViewData);
return new ViewViewComponentResult
{
ViewEngine = ViewEngine,

View File

@ -82,6 +82,25 @@ namespace Microsoft.AspNetCore.Mvc
Assert.Null(actualResult.ViewName);
}
[Fact]
public void ViewComponent_View_WithEmptyParameter_SetsViewDataModelWithDefaultViewName()
{
// Arrange
var viewComponent = new TestViewComponent();
var model = new object();
viewComponent.ViewData.Model = model;
// Act
var actualResult = viewComponent.View();
// Assert
Assert.IsType<ViewViewComponentResult>(actualResult);
Assert.NotSame(viewComponent.ViewData, actualResult.ViewData);
Assert.Equal(new ViewDataDictionary<object>(viewComponent.ViewData), actualResult.ViewData);
Assert.Same(model, actualResult.ViewData.Model);
Assert.Null(actualResult.ViewName);
}
[Fact]
public void ViewComponent_View_WithViewNameParameter_SetsResultViewWithCustomViewName()
{