CSharpBeginner
CSharpBeginner

Reputation: 1725

Passing string to anonymous object to Tag Helper

Usage of my tag helper:

<website-information info="new WebInfo { Age = 55, Author = "Test" }"></website-information>

How to pass Author string property correctly? When I write Author = " it thinks that the info attribute should look like this:

new WebInfo { Age = 55, Author = "

I got compilation error

Upvotes: 1

Views: 415

Answers (2)

itminus
itminus

Reputation: 25360

Razor allow us to use @ to evaluate C# expression. So you could use @ to get an complex object.

As for your scenario, you could simply use the following code:

<website-information info='@(new WebInfo{Age=22,Author="author1"})'></website-information>

<website-information info="@(new WebInfo{Age=33,Author="author2"})"></website-information>

Upvotes: 2

Jason Roner
Jason Roner

Reputation: 905

You can't just throw in a complex object. Depending on how exactly you want the output to look like on the client side, let's say:

<website-information info="Age:55,Author:Test"></website-information>

Then you would do this:

<website-information info="@("Age:55,Author:Test")"></website-information>

or:

@{
   var webInfo = new WebInfo { Age = 55, Author = "Test" };

   <website-information info="@("Age:" + webInfo.Age + ",Author:" + webInfo.Author)"></website-information>

}

Upvotes: 0

Related Questions