Improvisation

Improvising the previous code

let us do the below improvisations, now that we have understood the basics

Code behind

using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;

public class IndexModel : PageModel
{
    [BindProperty]
    public int FirstNum { get; set; }
    
    [BindProperty]
    public int SecondNum { get; set; }
    
    public int Result { get; set; }
    
    public void OnGet(){ }

    public void OnPost()
    {
        Result = FirstNum + SecondNum;        
    }
}

Mark up

@page
@model IndexModel

<form method="post">
    @Html.AntiForgeryToken()

    <label>Enter the first number:</label>
    <input type="number" name="FirstNum" required>

    <label>Enter the second number:</label>
    <input type="number" name="SecondNum" required>

    <button type="submit">Add</button>
</form>


@if(@Model.Result!=0){
    <p>Result of adding @Model.FirstNum and @Model.SecondNum is @Model.Result</p>
}

What changes did you notice?

Last updated