Hi All,
I have created a simple ASP.NET Core 2 Razor Pages application. It adds two numbers and show result to the user.The problem is that I can't post the model to PageModel class. Can anyone help ? My code is below
Razor Page.
@page @model WebApplication18.Models.MathsCalc @{ }<h2>Simple Calculation In Razor Pages</h2><form method="post"><div asp-validation-summary="All"></div><div><div><label asp-for="FirstNumber"></label><input asp-for="FirstNumber" /><span asp-validation-for="FirstNumber"></span></div></div><div><div><label asp-for="SecondNumber"></label><input asp-for="SecondNumber" /><span asp-validation-for="SecondNumber"></span></div></div><div><button type="submit">Show Result</button></div><div><label asp-for="Result"></label></div></form>
Model
public class MathsCalc { [Required(ErrorMessage = "First Number is required.")] public decimal FirstNumber { get; set; } [Required(ErrorMessage = "Second Number is required.")] public decimal SecondNumber { get; set; } public decimal Result { get; set; } }
PageModel
public class IndexModel : PageModel { public IActionResult OnGet() { return Page(); } public IActionResult OnPost(MathsCalc mc) { if (ModelState.IsValid) { mc.Result = mc.FirstNumber + mc.SecondNumber; } return Page(); } }
Thanks.