Fix #231; add `ViewBag` to `ViewComponent`

- update MVC sample to demonstrate use and that `ViewBag` is scoped
- nit: add a bit of padding between border and tag cloud words
This commit is contained in:
dougbu 2014-04-12 13:33:51 -07:00
parent 0e7dff9ca7
commit 271c849923
4 changed files with 19 additions and 1 deletions

View File

@ -37,6 +37,7 @@ namespace MvcSample.Web.Components
private string[] GetTags(int count)
{
@ViewBag.Title = count.ToString() + " Tags:";
return Tags.Take(count).ToArray();
}
}

View File

@ -1,6 +1,7 @@
@model string[]
<div style="width: 300px; height: 300px">
<div style="width: 300px; height: 250px; padding: 0px 10px;">
<h4>@ViewBag.Title</h4>
@foreach (var tag in Model)
{
<span>@tag</span>

View File

@ -134,6 +134,7 @@
<div style="float: right; border: 5px solid red;">
@await Component.InvokeAsync("Tags", 15)
<p style="padding: 0px 10px;">'@ViewBag.Title' should match page heading (still)</p>
</div>
</div>

View File

@ -7,6 +7,8 @@ namespace Microsoft.AspNet.Mvc
[ViewComponent]
public abstract class ViewComponent
{
private dynamic _viewBag;
public HttpContext Context
{
get { return ViewContext == null ? null : ViewContext.HttpContext; }
@ -14,6 +16,19 @@ namespace Microsoft.AspNet.Mvc
public IViewComponentResultHelper Result { get; private set; }
public dynamic ViewBag
{
get
{
if (_viewBag == null)
{
_viewBag = new DynamicViewData(() => ViewData);
}
return _viewBag;
}
}
public ViewContext ViewContext { get; set; }
public ViewDataDictionary ViewData { get; set; }