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

ViewDataDictionary does not get activated via Activate attribute in TagHelpers #1732

Closed
ajaybhargavb opened this issue Dec 18, 2014 · 5 comments

Comments

@ajaybhargavb
Copy link
Contributor

In this scenario, the ViewDataDictionary is not getting activated.

public class DummyTagHelper : TagHelper
{
    public string Expression { get; set; }

    [Activate]
    public ViewDataDictionary ViewDataDictionary { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        if (!string.IsNullOrEmpty(Expression))
        {
            output.Content = (string)ViewDataDictionary.Eval(Expression);
        }
    }
}
@pranavkm pranavkm changed the title Can't inject ViewDataDictionary with Activate attribute ViewDataDictionary does not get activated via Activate attribute in TagHelpers Dec 18, 2014
@dougbu
Copy link
Member

dougbu commented Dec 18, 2014

Currently DefaultTagHelperActivator injects ViewContext and anything available in DI. Since ViewContext contains a reference to the ViewDataDictionary and an isolated (per-helper) ViewDataDictionary isn't very useful, what exactly is missing?

Note users can give themselves a ViewData property using

[Activate]
public ViewContext ViewContext { get; set; }

private ViewDataDictionary ViewData
{
  get
  {
    return ViewContext?.ViewData;
  }
}

@pranavkm
Copy link
Contributor

It ends up being inconsistent with activation in Razor pages and View components.
Acitvating a ViewDataDictionary works in both these scenarios versus for tag helpers it produces an exception stating the service could not be located. We should attempt to have the services we activate be consistent.

@dougbu
Copy link
Member

dougbu commented Dec 18, 2014

RazorPageActivator won't activate a ViewContext property (handles only ViewDataDictionary properties and services from DI). And DefaultControllerActivator handles a few additional properties hanging off the main ActionContext it activates -- HttpContext, HttpRequest, and HttpResponse. So what is the consistency goal?

Further support for activating ViewDataDictionary properties is a bit problematic because such properties set up easy inconsistencies e.g. between RazorPage.ViewData and RazorPage.ViewContext.ViewData. Since the ViewContext.ViewData property is settable, would be more consistent for ViewDataDictionary properties in RazorPage, ITagHelper, or ViewComponent to read / write through to ViewContext.

@pranavkm
Copy link
Contributor

RazorPageActivator won't activate a ViewContext property (handles only ViewDataDictionary properties and services from DI). And DefaultControllerActivator handles a few additional properties hanging off the main ActionContext it activates -- HttpContext, HttpRequest, and HttpResponse. So what is the consistency goal?

Shouldn't we address the lot in that case?

@ajaybhargavb
Copy link
Contributor Author

9fcf31f

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants