Home > Software engineering >  When I Adding to [HttpPost] My Code Local Host Does Not Showing Frontend
When I Adding to [HttpPost] My Code Local Host Does Not Showing Frontend

Time:07-08

I'm trying to create a simple calculator but when i adding to [HttpPost] to the controller before IActionResult Calculator, it does not showing any response in browser. Without [HttpPost] i can see frontend part but i can't get any result for result. This is my controller code:

using System.Diagnostics;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View(new SimpleCalculatorModels());
        }

        public IActionResult Calculator(SimpleCalculatorModels model, string method)   
        {
            if (method == "addition")
            {
                model.ResultNumber = model.FirstNumber   model.SecondNumber;
            }
            if (method == "substraction")
            {
                model.ResultNumber = model.FirstNumber - model.SecondNumber;
            }
            if (method == "multiplication")
            {
                model.ResultNumber = model.FirstNumber * model.SecondNumber;
            }
            if (method == "division")
            {
                model.ResultNumber = model.FirstNumber / model.SecondNumber;
            }

            return View(model);
        }

    }
}

And this is my View class:


@model WebApplication1.Models.SimpleCalculatorModels
@{

}


<body>
    <h2>SIMPLE CALCULATOR</h2>
    <div class = "border-dark">
       
           <div class = "form-group">
             <label for = "FirstNumber">First Number</label>
             <input type="text"  id="FirstNumber" name="FirstNumber" value="@Model.FirstNumber"/> 
           </div>

           <div class = "form-group">
             <label for = "SecondNumber">Second Number</label>
             <input type="text"  id="SecondNumber" name="SecondNumber" value="@Model.SecondNumber"/> 
           </div>

           <button type= "submit"  id="addition" value="addition" name="calculate"> </button>
           <button type= "submit"  id="substraction" value="substraction" name="calculate">-</button>
           <button type= "submit"  id="multiplication" value="multiplication" name="calculate">*</button>
           <button type= "submit"  id="division" value="division" name="calculate">/</button>

            <div class = "form-group">
             <label for = "Result">Result</label>
             <input type="text"  id="ResultNumber" name="ResultNumber" value="@Model.ResultNumber"/> 
           </div>



    </div>
</body>

CodePudding user response:

Model

public class SimpleCalculatorModels
{
    public SimpleCalculatorModels()
    {
        FirstNumber = 0;
        SecondNumber = 0;
        ResultNumber = 0;

    }
    public double SecondNumber { get; set; }
    
    public double FirstNumber { get; set; }

    public double ResultNumber { get; set; }
}

Controller

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }


    public IActionResult Index(SimpleCalculatorModels model)
    {
        return View(model); 
    }
    
    
    [HttpPost]
    public IActionResult Calculator(SimpleCalculatorModels model,string method)
    {
        if (method == "addition")
        {
            model.ResultNumber = model.FirstNumber   model.SecondNumber;
        }
        if (method == "substraction")
        {
            model.ResultNumber = model.FirstNumber - model.SecondNumber;
        }
        if (method == "multiplication")
        {
            model.ResultNumber = model.FirstNumber * model.SecondNumber;
        }
        if (method == "division")
        {
            model.ResultNumber = model.FirstNumber / model.SecondNumber;
        }

        return RedirectToAction("Index", model);
    }


}

Index.cshtml

@model SimpleCalculatorModels

<h2>SIMPLE CALCULATOR</h2>
<form asp-action="Calculator" method="post">

    <div class = "border-dark">

               <div class = "form-group">
                 <label for = "FirstNumber">First Number</label>
                 <input type="text"  id="FirstNumber" name="FirstNumber" value="@Model.FirstNumber"/>
               </div>

               <div class = "form-group">
                 <label for = "SecondNumber">Second Number</label>
                 <input type="text"  id="SecondNumber" name="SecondNumber" value="@Model.SecondNumber"/>
               </div>

               <button type= "submit"  id="addition" value="addition" name="method"> </button>
               <button type= "submit"  id="substraction" value="substraction" name="method">-</button>
               <button type= "submit"  id="multiplication" value="multiplication" name="method">*</button>
               <button type= "submit"  id="division" value="division" name="method">/</button>

                <div class = "form-group">
                 <label for = "Result">Result</label>
                 <input type="text"  id="ResultNumber" name="ResultNumber" value="@Model.ResultNumber"/>
               </div>
    </div>

</form>

Demo

enter image description here

Updated=========================================

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

  //other actions

   //this get action will load the Calculator.cshtml page
    public IActionResult Calculator(SimpleCalculatorModels model)
    {
        return View(model);
    }
    
    //this post action will do some calculations and return the value 
    [HttpPost]
    public IActionResult Calculator(SimpleCalculatorModels model,string method)
    {
        if (method == "addition")
        {
            model.ResultNumber = model.FirstNumber   model.SecondNumber;
        }
        if (method == "substraction")
        {
            model.ResultNumber = model.FirstNumber - model.SecondNumber;
        }
        if (method == "multiplication")
        {
            model.ResultNumber = model.FirstNumber * model.SecondNumber;
        }
        if (method == "division")
        {
            model.ResultNumber = model.FirstNumber / model.SecondNumber;
        }

        return View(model);
    }
}

Calculator.cshtml

Same as the index.cshtml i provided.

  • Related