Question

I can call a method which accepts an HTML/Razor block as a parameter like so:

@Html.SiteText(@<div>Some content</div>)

Calling the same method with multiple top level tags fails due to a parser error. I.E.

@Html.SiteText(@<div>Some content</div><div>Some more content</div>)

fails with: Compiler Error Message: CS1026: ) expected

Is there any way to pass a Razor helper which has multiple top level tags?

Edit:

Here's the signature of the SiteText extension method:

public static IHtmlString SiteText(this HtmlHelper htmlHelper, Func<object, HelperResult> content)

The actual implementation of the method shouldn't matter, as this applies to how the method is invoked rather than what the implementation does with the inputs.

Was it helpful?

Solution

The simple way to look at it is that the Razor view engine is looking at each of those div tags as a single property.

Wrapping them in a text tag should resolve the issue:

@Html.SiteText(@<text><div>Some content</div><div>Some more content</div></text>)

OTHER TIPS

Not tested but I suspect this will solve it:

@Html.SiteText(@<text>
    <div>Some content</div>
    <div>Some more content</div>
</text>)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top