locked
Two helpers that set IsPost, code getting confused RRS feed

  • Question

  • User2117521848 posted

    I have some app code that uses two helpers. Each helper uses "method=post" internally. My intent was to call helper A followed by a call to helper B. However the fact that IsPost is set in helper A is causing helper B to see IsPost as already set, causing helper B to exit without ever putting up a form.

    I believe that IsPost is a system property so setting it to false after helper A runs seems like a really bad idea. Any thoughts ?

    Wednesday, April 27, 2016 8:06 PM

Answers

  • User-821857111 posted

    I can't see where you are trying to set a value for IsPost (which as you guessed, you can't). In any event, if you want a helper to run under certain conditions, wrap the call to the helper in that condition:

    if(IsPost){
        @MyHelpers.MyHelper()
    }

    You shouldn't prefix variable names with the @ sign inside codeblocks. You only prefix them with @ when you want to render them to the browser. Also, in the code that calls your helpers, you seem to have the braces inverted. Helpers are used to render content, so they shouldn't be in code blocks or need to be terminated with semicolons.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2016 1:01 PM
  • User2117521848 posted

    Hello Mike,

    Thanks for your input. I believe you have pointed out the crux of the problem - what I am trying to do does not belong in a helper. I will be moving these functions into utility pages which I can invoke from other app logic.

    Thanks again,

    Rich

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 29, 2016 12:19 PM

All replies

  • User-821857111 posted

    Could you show the code of the helpers and the code that attempts to make use of them, please?

    Thursday, April 28, 2016 7:46 AM
  • User2117521848 posted

    Sure. Here are the two helpers:

    @helper TableOfStrings (int stringcount, string namebase, string labelbase, string legend) {
     
        if (IsPost) {
            return;
        }
     
    <form method="post" action="">
     
            <h1>@legend</h1>
            <ol>
                @for (int j=1;j<=@stringcount;j++) {
                    string jname = j.ToString();
                    string classname = @namebase + jname;
                    string labelname = @namebase + jname;
                    string idname = @namebase + jname;
                    string boxname = @namebase + jname;
                    string thislabelname = @labelbase + jname;
                    <li class=@classname>
                    <label for=labelname>@thislabelname</label>
                    <input type="text" id=@idname name=@boxname />
                    </li>
                }
            </ol>
            <input type="submit" value="Continue" />
     
    </form>
    }

    @helper FTFFEmail (string initemail, string emailname) {
        if (IsPost) {
            return;
        }
    }
    <body>
        <h2>Compose Email</h2>
        <form method="post">
            <div>
                <textarea name="@bodyemail" cols="80" rows="20">@initemail</textarea>
            </div>
            
            <div>
                <input type="submit" value="Send" />
            </div>
        </form>
    </body>
    }
    And here is the invoking code:

        string namebase = "emailaddress";
        string labelbase = "Email Address ";
        string legend = "Enter Up To 10 Email Addresses";
        List<string> emailaddresslist = new List<string>();
     
        {
            @MyHelpers.TableOfStrings(stringcount, namebase, labelbase, legend);
        }
     
     
        if (IsPost) {
            Functions.FTFFDebug ("FTFFutilJoinSite: after call to TableOfStrings IsPost is still true");
        }
     
        for (int j=1;j<=@stringcount;j++) {
            jname = j.ToString();
            boxname = namebase + jname;
            boxvalue = Request[boxname];
            if (!boxvalue.IsEmpty()) {
                Functions.FTFFDebug ("boxname is " + boxname);
                Functions.FTFFDebug ("boxvalue is " + boxvalue);
                emailaddresslist.Add (boxvalue);          
            }
     
        }
     
        // Now get the email
        initemail = "Hello";
    
     
        {
            @MyHelpers.FTFFEmail(initemail, emailname);
        }
     
    Thursday, April 28, 2016 11:34 AM
  • User-821857111 posted

    I can't see where you are trying to set a value for IsPost (which as you guessed, you can't). In any event, if you want a helper to run under certain conditions, wrap the call to the helper in that condition:

    if(IsPost){
        @MyHelpers.MyHelper()
    }

    You shouldn't prefix variable names with the @ sign inside codeblocks. You only prefix them with @ when you want to render them to the browser. Also, in the code that calls your helpers, you seem to have the braces inverted. Helpers are used to render content, so they shouldn't be in code blocks or need to be terminated with semicolons.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2016 1:01 PM
  • User2117521848 posted

    Hello Mike,

    Thanks for your input. I believe you have pointed out the crux of the problem - what I am trying to do does not belong in a helper. I will be moving these functions into utility pages which I can invoke from other app logic.

    Thanks again,

    Rich

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 29, 2016 12:19 PM