1

I'm trying to create the following Helper using the @helper utility present in ASP.NET MVC. The idea is to create a Panel and call to this helper from a page for set the title and the body of the panel (I want to have more than one panel on the same page for this reason I can't use layout).

Here is the helper:

@helper PanelHelper(string title, string body){
    <fieldset class="fieldset-border-2">
        <legend style="display: none;">Edit</legend>
        <div style="display: table;">
            <div style="display: table-row;">
                <div class="panel-first-row" style="display: table-cell;">
                    <div style="border-bottom: 1px solid #cecece; padding-left: 5px; height: 25px;">
                        <div style="display: table; margin: 9px 0 9px 0;">
                            <div style="display: table-row;">
                                <div class="title" style="display: table-cell;">
                                    @title
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="edit-row-separator" style="display: table-row;">
            </div>
            <div style="display: table-row;">
                <div style="display: table-cell;">
                    @body
                </div>
            </div>
        </div>
    </fieldset>
}

And here how I would like to call to this helper:

@Helpers.PanelHelper("Customers")
{
    <div style="display: table; width: 100%;">
        <div style="display: table-row; width: 100%;">
           ................ HTML => Customer Fields ......................
        </div>
    </div>
}
1
  • Actually I'm not able to call the helper in that way (using the { })
    – erickt
    Jul 17, 2012 at 19:51

1 Answer 1

Reset to default
0

You really should be calling it like this:

@{
   string body = "<div style=\"display: table; width: 100%;\">
        <div style=\"display: table-row; width: 100%;\">
           ................ HTML => Customer Fields ......................
        </div>
    </div>";
}


@PanelHelper("Customers", body)

Look here for more information: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx

4
  • I saw that approach before, but I don't like the idea of have the HTML code as a string, I would like to have both codes (Panel template and page) as pure HTML
    – erickt
    Jul 18, 2012 at 13:27
  • I don't like putting HTML in code either, but I'm not sure of an easier way to do what you want. Could you simply use some conditional logic in the Helper to show/hide regions based on what's passed in as the title? Jul 18, 2012 at 16:11
  • No, I only want to create a simple Panel like APS.NET <Panel></Panel>
    – erickt
    Jul 19, 2012 at 20:49
  • Good question. How does Microsoft acheive the @using BeginForm scenario?
    – Jacques
    Sep 4, 2012 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.