Joe herman
Joe herman

Reputation: 134

how to share data between taghelpers?

I have a class that contains two tagHelpers.

[htmlTargetElement("div")]
public class DivTagHelper :TagHelpers {
public override void process(TagHelperContext context, TahHelperOutput output)
 //codes
}

[htmlTargetElement("button", ParentTag="div")]
public class ButtonTagHelper :TagHelpers {
public override void process(TagHelperContext context, TahHelperOutput output)
 //codes
}

One acts on the DIV and the other acts on the Buttons. I need to generate values in one of the tagHelpers(Div) and share them for other tagHelpers(Button).

There is a solution to this?

Upvotes: 1

Views: 329

Answers (1)

Morteza Jangjoo
Morteza Jangjoo

Reputation: 1790

you must use TagHelperContext.items property.

This property is a collection of dictionary that can act as a place to share data between taghelpers.

[htmlTargetElement("div")]
public class DivTagHelper :TagHelpers {
public override void process(TagHelperContext context, TahHelperOutput output)
 context.Items["myData"]="somethings";
}


[htmlTargetElement("button", ParentTag="div")]
public class ButtonTagHelper :TagHelpers {
public override void process(TagHelperContext context, TahHelperOutput output)
 string strName=context.Items["myData"];
}

Upvotes: 4

Related Questions