Reputation: 4455
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 :
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()
Upvotes: 1
Views: 1186
Reputation: 51
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>
.
Upvotes: 0
Reputation: 3312
I just cloned your app and it's working fine. It's saving the data in InMemoryDatabase as needed. You can see the result in cmd .
see the other foto to for the breakpoint
Upvotes: 1