简体   繁体   中英

How do I pass multiline HTML as parameter

I need to pass HTML as Parameter to a Html Helper Method (eg: ClientTemplate in Telerik's Kendo UI MVC Wrapper).

Basicly I try to pass this:

<ul class="list-unstyled">
    <li>Foo</li>
    <li>Bar</li>
    <li>Baz</li>
</ul>

I tried this:

.Template(@<text>
    <ul class="list-unstyled">
        <li>Foo</li>
        <li>Bar</li>
        <li>Baz</li>
    </ul>
</text>.ToString()))

I get this error

CS1660 Cannot convert lambda expression to type 'string' because it is not a delegate type

I know it is possible with "...<li>Foo</li><li>Bar</li>..." but I am wondering if there is a better method like razors <text>

Thanks to Ashish Emmanuel's comment , I have this solution:

Helper Method

public static string RazorTemplateHelper(Func<object, HelperResult> template)
{
    return template.Invoke(null).ToString();
}

Razor

.Template(RazorTemplateHelper(@<text>
    <ul class="list-unstyled">
        <li>Foo</li>
        <li>Bar</li>
        <li>Baz</li>
    </ul>
</text>))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM