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.

有帮助吗?

解决方案

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>)

其他提示

Not tested but I suspect this will solve it:

@Html.SiteText(@<text>
    <div>Some content</div>
    <div>Some more content</div>
</text>)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top