locked
Is Possible ? Post without Submit ? RRS feed

  • Question

  • User518677258 posted

    I have a button

    <div class="col-1" >
        <form  method="post">
            <button class="btn btn-outline-primary">Toggle Model  @Model.isX </button>
        </form>
    </div>

    While this does get to my OnPost()  I need to send the value to toggle  it  as in OnPost(bool isX )

    All the examples I see have the inputs that generate the parameters for the post followed by a  submit.

    What can I do to simply post the Boolean from my button & bypass the input submit button model ?

    Thanks

    Monday, October 12, 2020 1:06 AM

Answers

  • User518677258 posted

    I used the value of the Submit Text to carry my info ( using a hidden input I cannot update the value, I understand that's by design   )

    The cshtml simply reduced to :

    <div class="col-1">
        <form method="post">
            @*<input type="hidden" asp-for="@Model.isToggle" />*@
            <input type="submit" value="Toogle from @Model.plModel " name="foo" class="btn btn-outline-primary" />
        </form>
    </div>

    the Post is easy :

      public  void OnPost(string  foo)
            {
                bool myBool = true;
              
                if (foo.Contains("A"))
                {
                    myBool = false;
                }
                 OnGet(myBool) ; 
            }

    The OnGet  -> and binding became simply

    [BindProperty(SupportsGet = true)]
            public string plModel { get; set; }

            public async void OnGet(bool isA=true )
            {
                if (isA)    // default page load
                {
                    plModel = "A";
                    List_A... = await Get_A....  // displays choices from A
                }
                else
                {
                    plModel = "B";
                    List_B= await Get_B… // displays choices from B
                }
               
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2020 3:29 AM

All replies

  • User518677258 posted

    There is one way that this might be done  - need to check syntax - 

    <form  method="post">
     <input type="hidden" id="isX" name="isX" value=@Model.isX>

    <input type="submit" value="Toggle Model X value from @Model.isX >
     </form>

    Monday, October 12, 2020 2:33 AM
  • User711641945 posted

    Hi andrewcw,

    What about using ajax?

    Here is a working demo like below:

    <form method="post">
        <button class="btn btn-outline-primary" type="button">Toggle Model  @Model.isX </button>
    </form>
    
    @section Scripts
    {
        <script>
            $("button").click(function () {
                var data = $('button').text().replace("Toggle Model","");
                alert(data);
                $.ajax({
                    type: "POST",   
                    url:"yoururl", //if you are in Index.cshtml and post the data to the OnPost method in 
    //Index.cshtml.cs,no need to write the url. headers: { RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val() }, data: { isX: data }, success: function (res) { //... }, }); }); </script> }

    Index.cshtml.cs:

    public void OnPost(bool isX)
    {   
        //....       
    }

    Best Regards,

    Rena

    Monday, October 12, 2020 5:29 AM
  • User518677258 posted

    Thanks for providing a code snippet.  I think for the given case I have a simpler method

    Tuesday, October 13, 2020 3:16 AM
  • User518677258 posted

    I used the value of the Submit Text to carry my info ( using a hidden input I cannot update the value, I understand that's by design   )

    The cshtml simply reduced to :

    <div class="col-1">
        <form method="post">
            @*<input type="hidden" asp-for="@Model.isToggle" />*@
            <input type="submit" value="Toogle from @Model.plModel " name="foo" class="btn btn-outline-primary" />
        </form>
    </div>

    the Post is easy :

      public  void OnPost(string  foo)
            {
                bool myBool = true;
              
                if (foo.Contains("A"))
                {
                    myBool = false;
                }
                 OnGet(myBool) ; 
            }

    The OnGet  -> and binding became simply

    [BindProperty(SupportsGet = true)]
            public string plModel { get; set; }

            public async void OnGet(bool isA=true )
            {
                if (isA)    // default page load
                {
                    plModel = "A";
                    List_A... = await Get_A....  // displays choices from A
                }
                else
                {
                    plModel = "B";
                    List_B= await Get_B… // displays choices from B
                }
               
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2020 3:29 AM