Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Change ViewComponent.View() to flow the ViewData.Model.
Browse files Browse the repository at this point in the history
- This is more consistent with how controllers work.

#4882
  • Loading branch information
NTaylorMullen committed Aug 25, 2016
1 parent c942eab commit 7036e2b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/Microsoft.AspNetCore.Mvc.ViewFeatures/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public virtual ViewResult View(object model)
[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;
Expand Down Expand Up @@ -195,7 +194,6 @@ public virtual PartialViewResult PartialView(object model)
[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;
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ public ViewViewComponentResult View<TModel>(TModel model)
/// <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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ public void ViewComponent_View_WithEmptyParameter_SetsResultViewWithDefaultViewN
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()
{
Expand Down

0 comments on commit 7036e2b

Please sign in to comment.