locked
Code cannot find newly created helper RRS feed

  • Question

  • User2117521848 posted

    Hello,

    I have written a helper which I would like to use in my site. The helper lives in a source file named MyHelpers.cshtml which exists in the App_Code folder. Here is the source for the helper:

    @helper TableOfStrings (string legend) {
        if (IsPost) {
            return;
        }
     
    <form method="POST" action="">
        <fieldset>
            <legend>@legend</legend>
            <ol>
                <li class="emailaddress1">
                    <label for="emailaddress1">Email address 1</label>
                    <input type="text" id="emailaddress1" name="emailaddress1" />
                </li>
            </ol>
            <input type="submit" value="Continue" />
        </fieldset>
    </form>
    }

    I try to call the helper from my site code, as follows:

        string legend = "";
     
        legend = "Enter Email Addresses";
     
        @MyHelpers.TableOfStrings (legend);

    The result is the following error message(s):

    Compilation Error              Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.            

    Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

    I truly have no idea what I am doing wrong. Help ?????
    Monday, April 25, 2016 4:42 PM

Answers

  • User2117521848 posted

    So the problem was in the invoking code. I changed it from:

    @MyHelpers.TableOfStrings (legend);

    to

    {

    @MyHelpers.TableOfStrings(legend);

    }

    and now it works fine. Thanks for the help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 27, 2016 3:00 PM

All replies

  • User-1327394822 posted

    i removed the fieldset and legend tag... and placed the @legend in h1 tag.

    Try removing this:

    <fieldset>
            <legend>@legend</legend>
    Tuesday, April 26, 2016 7:59 AM
  • User2117521848 posted

    Gautam, I did what you suggested, changing the helper code to:

    @helper TableOfStrings (string legend) {
        if (IsPost) {
            return;
        }
     
    <form method="POST" action="">
     
            <h1>@legend</h1>
            <ol>
                <li class="emailaddress1">
                    <label for="emailaddress1">Email address 1</label>
                    <input type="text" id="emailaddress1" name="emailaddress1" />
                </li>
            </ol>
            <input type="submit" value="Continue" />
     
    </form>
    }

    and keeping the call to the helper as:
        string legend = "";
     
        legend = "Enter Email Addresses";
     
        @MyHelpers.TableOfStrings (legend);


    I still get the same error. ????
     
    Tuesday, April 26, 2016 11:56 AM
  • User2117521848 posted

    So the problem was in the invoking code. I changed it from:

    @MyHelpers.TableOfStrings (legend);

    to

    {

    @MyHelpers.TableOfStrings(legend);

    }

    and now it works fine. Thanks for the help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 27, 2016 3:00 PM