How to prevent the submit and post in ajax?

mc 3,681 Reputation points
2022-08-19T08:04:57.517+00:00

I am using asp.net core there is a list page and a add new button and a edit.cshtml page how to click the new button to get the data of edit page show it in list page as

modal and then when I click the submit button it will not post to the edit page but ajax to it?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,162 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Brando Zhang-MSFT 2,956 Reputation points Microsoft Vendor
    2022-08-22T02:55:26.2+00:00

    If you want to prevent the default post method for the form's submit button, I suggest you could consider using the event.preventDefault() method.

    More details, you could refer to below codes:

    <form id="user-form"   method="post">  
      First name: <input type="text" name="first_name" id="first_name"><br/>  
      Last name: <input type="text" name="last_name" id="last_name"><br/>  
      Phone number: <input type="text" name="phone_number" id="phone_number"><br/>  
      <input type="submit" id="submit-btn">  
    </form>  
    

    JS:

    document.getElementById("submit-btn").addEventListener("click", function(event){  
      event.preventDefault()  
    });  
    
    0 comments No comments