DaveDev
DaveDev

Reputation: 42175

How can I call an 'Action' from a RazorEngine cshtml template?

I'm using RazorEngine (razorengine.codeplex.com).

How can I get this to call an 'Action' from my cshtml templates? I quote 'Action' because it's not really an Action in the MVC sense, as RazorEngine is detached from MVC and I'm running this from a WPF solution. Basically what I need is something as follows: (please note what follows is strictly pseudocode intended to illustrate an idea)

<!DOCTYPE html>
<html>
    <head></head>
    <body>

        <!-- something similar to the following so that I can 
             include partial views where needed. -->
        @Html.Action("Method1")

        @Html.Action("Method2")

<!-- or -->

        @Html.Action("RazorTemplate1.cshtml")

        @Html.Action("RazorTemplate2.cshtml")

<!-- or -->

        @MyTemplateFunctionality.Method1()

        @MyTemplateFunctionality.Method2()
    </body>
</html>

Where the methods are defined, e.g.

public static class MyTemplateFunctionality
{
    public static string Method1(string templateName)
    {
        // execute functionality to render HTML output for Method1
        string htmlOutput = RazorEngine.Razor.Parse(templateName, new { ObjModelForMethod1 }); ;

        return htmlOutput;
    }

    public static string Method2(string templateName)
    {
        // execute functionality to render HTML output for Method2
        string htmlOutput = RazorEngine.Razor.Parse(templateName, new { ObjModelForMethod2 }); ;

        return htmlOutput;
    }
}

Is this possible? If so, what do I need to do?

Upvotes: 0

Views: 1724

Answers (1)

Mathew Thompson
Mathew Thompson

Reputation: 56429

You could achieve that by doing:

@Html.Method1("yourtemplatename")

@Html.Method2("yourtemplatename")

But, you'd have to make your Method1 and Method2 extension methods:

public static string Method1(this HtmlHelper htmlHelper, string templateName)

public static string Method2(this HtmlHelper htmlHelper, string templateName)

OP Edit

Yes, this is the right idea. As you were answering it, I was poking around a bit more and I found a related problem which answers this question completely, so I decided to include the complete answer here (as part of your answer so I could mark it correct!), for future reference.

The complete answer is:

namespace MyCompany.Extensions
{
    public static class MyClassExtensions
    {
        public static string ExtensionMethod1(this MyClass myClass)
        {
            myClass.DoStuff();
            return "whatever I want my string to be";
        }

        public static string ExtensionMethod2(this MyClass myClass)
        {
            myClass.DoOtherStuff();
            return "the output of ExtensionMethod2";
        }
    }

    public class MyClass
    {
        public void DoStuff()
        {
            // do whatever
        }

        public void DoOtherStuff()
        {
            // do whatever else
        }
    }
}

Then in the cshtml, simply add:

@using MyCompany.Extensions
@using MyCompany

@{
    var myInstance = new MyClass();
    @myInstance.ExtensionMethod1()

    @myInstance.ExtensionMethod1()
}

Upvotes: 2

Related Questions