문제

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