Ajax call from Asp.net Razor page

Agha Khan 166 Reputation points
2023-04-30T16:03:16.64+00:00

I have a Register.cshtml page where I also like to add a close button outside the Form.

<button type="button" id="MyCloseBtn" class="btn-close" method="post" aria-label="Close" style="margin-left: auto; margin-right: 0;"></button> Do not see any issue.

The idea is when the close button is clicked; it should switch to the index page. For some reason, the click event is always activated without connecting. Why? I just want to switch when the close button is clicked.

https://1drv.ms/f/s!AtjIdnVOoJXtpNthBNsruHOX_s8Riw?e=SCbjis

<script type="text/javascript" language="javascript">

        const mycomp = document.getElementById('MyCloseBtn');
        mycomp.addEventListener('click', (e) => loginButtonClickedHandler(e));


        function loginButtonClickedHandler(e) {
        @{
            // like to RedirectToPage("./Index");
        }
            

        }
    </script>
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-04-30T18:02:06.8333333+00:00

    I thing you are asking how to write a JavaScript redirect. https://developer.mozilla.org/en-US/docs/Web/API/Window/location

    window.location = "/Index";
    
    0 comments No comments

  2. Agha Khan 166 Reputation points
    2023-05-03T05:06:36.78+00:00

    in xxx.cshtml

    I want to close the form. The best way is to bring a close button inside the Form and visually place close button at the desired location with margins. Use button attribute asp-page-handler="BackToIndex".

    and in xxx.cshtml.cs

    public IActionResult OnPostBackToIndex()
            {
                return RedirectToPage("/Index");
            }
    

    It works fine. Please note you have to add OnPost part of the function.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.