Skip to content
Permalink
Browse files

Step 1: A simple form as you do

  • Loading branch information
haacked committed Nov 20, 2019
1 parent cfa4158 commit b431650bb6cfb75d9560bf9caeaf360f438d7dba
Showing with 33 additions and 5 deletions.
  1. +13 −0 src/Pages/Index.cshtml
  2. +20 −5 src/Pages/Index.cshtml.cs
@@ -9,3 +9,16 @@
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<div id="the-message" class="flash">
@Model.TheMessage
</div>

<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="@Model.TheValue"></label>
<input asp-for="@Model.TheValue" />
<span asp-validation-for="@Model.TheValue" class="text-danger"></span>
</div>
<input type="submit" value="Submit"/>
</form>
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
@@ -17,9 +14,27 @@ public IndexModel(ILogger<IndexModel> logger)
_logger = logger;
}

public void OnGet()
[Required]
[BindProperty]
public string TheValue { get; set; }

public string TheMessage { get; private set; }

public IActionResult OnGet()
{
TheMessage = "Nothing yet";
return Page();
}

public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return OnGet();
}

TheMessage = $"Yay, you posted '{TheValue}'";
return Page();
}
}
}

0 comments on commit b431650

Please sign in to comment.
You can’t perform that action at this time.