Click here to Skip to main content
15,887,240 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I really like the progress am making learning ASP.NET. Yesterday I started off and learnt that you need to have two folders in your ASP project where one(Pages) contains the ASP Razor pages and the other one(wwwroot) contains the styling files for the razor pages. I also learnt that the entire behavior of the page displayed to the user is controlled by the code behind file in the startup C# code file, like you cannot be able to use bootstrap and custom css styles until you check the line
C#
app.UseStaticFiles();

Now I have created a bootstrap form with the fields name and description with inputs for each inside a modal and then gave them asp identifiers so that the model file can receive these values when the form post action is called. I created a Models folder and added a new class file called IndexModel and then declared two getters and setters for the name and query called Name and Description respectively. Check the definitions for all the relevant files in the code block section below.

What I have tried:

This is my IndexModel.cs file
C#
public class IndexModel
  {
      public string Name { get; set; }
      public string Description { get; set; }
      //receive the values entered by the user through the onpost method
      public void OnPost()
      {
          Name = Name;
          Description = Description;

      }
  }

This is the form
HTML
<form onsubmit="return false;" action="post" id="form">
 <div class="form-group">
   <label for="name">Name</label>
     <input type="text" class="form-control" id="name" placeholder="Enter your name" asp-for="Name">
      </div>
      <div class="form-group">
  <label for="query">Query</label>
 <textarea class="form-control" id="query" style="margin-top:20px" rows="5" placeholder="Enter your query" asp-for="Description"></textarea>
                            </div>
     
</form>


I have handled the user input in my JavaScript file when I try to call this line
JavaScript
//means everything is okay, now pass the values entered to the ASP models
form. Submit();

I get the error
AmbiguousMatchException: The request matched multiple endpoints. Matches:


Wht is that error and how can I resolve this?
Posted
Updated 19-Jun-23 22:12pm

1 solution

Normally, this means that you have more than one route with the same POST signature, ie: same params.

Also, your Form properties are not correct. Refer to: Tag Helpers in forms in ASP.NET Core | Microsoft Learn[^]. According to the sample in the link provided, when the page is inspected, it should look like:
HTML
<form method="post" action="/Demo/Register">
    <!-- Input and Submit elements -->
    <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>">
</form>


UPDATE

Okay, It has been established that you are creating a WebForms ASP.NET Web App, Also known as a Razor Pages App[^].

Here is a good Tutorial to show you how to Post Data for this style of app: How to Submit (Post) Form in ASP.Net Core Razor Pages - YouTube[^].

UPDATE #2

For MVC[^], here is a tutorial for you: Handle Multiple Form Submit Buttons in ASP.NET Core MVC - ASP.NET 6 MVC - YouTube[^] and here is a website dedicated to teaching you MVC: The ASP.NET Core MVC Tutorial[^]
 
Share this answer
 
v4
Comments
Tim the Gamer 20-Jun-23 4:15am    
Yes, I got the action wrong. The action should point to the file that should process the form input, then the method should be post. Let me try and change those and see if it will work
Graeme_Grant 20-Jun-23 4:24am    
If this helped, which it appears to have, please mark the solution as accepted so that others are aware. It appears that there is no back end to handle the POST data. A link is provided below that explains how to make a MVC ASP.Net Core app.
Tim the Gamer 20-Jun-23 4:25am    
Let me inspect the tutorials in the attached link more because it's still throwing the ambiguous match exception error
Graeme_Grant 20-Jun-23 4:31am    
Only client-side code posted, no server-side code, so hard to debug from here.

Check the route both on the client and server. Also check your routing configuration and if any controller methods have the same params. You may have duplicate routes defined. Lastly, make sure that you have tagged the controller method as POST.
Tim the Gamer 20-Jun-23 4:32am    
I do not have any controller classes or folder in my project. Do I need a controller for a form?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900