asp.net core 2.0 not able to post a simple form

touseefbsb

I am trying to learn Asp.net Core 2.0 and following a tutorial, I created a very simple form with only 1 property, the name of the Customer. I debugged with breakpoints I am not even able to hit the PostAsync method in CreateModel

CreateModel

public class CreateModel : PageModel
{
    private readonly ApplicationDbContext _db;
    [BindProperty]
    public Customer Customer { get; set; }
    public CreateModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid) return Page();

        _db.Customers.Add(Customer);
        await _db.SaveChangesAsync();
        return RedirectToPage("/");
    }
}

Create.cs.html

@page
@model WebApplication1.Pages.CreateModel
@{
    ViewData["Title"] = "Create";
}

<h2>Create</h2>

<p>Enter your name: </p>

<form method="post">
    <div asp-validation-summary="All"/>
    <div>Name : <input asp-for="Customer.Name"/></div>
    <input type="submit"/>
</form>

@section Scripts{ 
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Customer

public class Customer
{
    public int Id { get; set; }
    [Required, StringLength(10)]
    public string Name { get; set; }

}

ApplicationDbContext

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions options) : base (options)    { }

    public DbSet<Customer> Customers { get; set; }
}

ConfigureServices method in my Startup class

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase("dbname"));
    services.AddMvc();
}

Update

Here is the full project on GitHub to reproduce the issue : https://github.com/touseefbsb/aspnet_post

When I enter more than 10 characters, it never hits the breakpoint in postasync method, and nothing happens on the page as well, it is supposed to show me the validation error on UI but it does not.

When I enter a name less than 10 character and press submit, it tries to submit ( the breakpoint in postasync is hit ) but then it tried to redirect, I get following page of error :

error stacktrace

Full StackTrace :

Microsoft.AspNetCore.Mvc.Internal.RedirectToPageResultExecutor.Execute(ActionContext context, RedirectToPageResult result)
Microsoft.AspNetCore.Mvc.RedirectToPageResult.ExecuteResult(ActionContext context)
Microsoft.AspNetCore.Mvc.ActionResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeResultAsync>d__19.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResultFilterAsync>d__24.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()
Ranopano

Check if Razor has generated an antiforgery token for you.

When you submit a form this token has to be validated and if your cookies don't have the expected one the submission gets rejected.

Update:

The RedirectToPage method requires full path to a page.

The form submission prevented by JavaScript when there is an input error but it won't show up because <div asp-validation-summary="All"> lacks the closing </div>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.Net Core 3 and simple POST the controller

ASP.NET Core MVC 3.1 form always null on POST

ASP.NET Core 2.1 Razor Form, Post not reaching Controller

ASP.Net Core - Get All data of a post form

Form post only a part of the model in ASP.NET Core

Post form to controller in ASP.NET Core MVC not working

asp.net core mvc form viewmodel empty on post

ASP.NET Core - Changing form values after a post

asp.net core web api Post form data with brackets

Asp.Net Core 1.1 ViewModel Null after Form Post

ASP.NET MVC Core - Post form to class parameter

not able to post my form from angular 6 app to asp.net web api

not able to fetch and post data with angular js using asp.net core mvc

Asp.net Core 2 API POST Objects are NULL?

ASP.NET Core 2, jQuery POST data null

ASP.NET core 2 how to skip identity association form?

Asp.net core rc2, I am able to authenticate with external login but i am redirected to this

ASP .NET Core (Net 6) POST Read Array from Form Data

Can't add a simple POST handler to an ASP.NET Core MVC web app

Post with int/string (simple type) in body to asp.net core web api 2.1 not working

How send simple value from axios.post to asp.net core?

Simple POST with Web Api .NET Core

How do you post form data and files using Ajax in asp.net.core?

asp.net core mvc application form post with query string parameters

Post form with only input buttons passing parameters to Action in ASP.NET Core

ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response

How do you post form data and files using Ajax in asp.net.core

ASP.NET Core Post form data IFormFile with ViewModel using HTTPClient

Problems Serializing a Form and Converting It to a Complex Class in Asp.net Core MVC to Post a Whole Model Using Ajax

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive