N
N
nordwind20132018-06-21 11:02:36
ASP.NET
nordwind2013, 2018-06-21 11:02:36

Calculator log output?

Here's the deal. I store the value of the result of the calculation in stat. sheet, then I try to deduce all values ​​in a certain broad gull. However, only the latter is displayed. Where did I fly?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Concurrent;
namespace FirstCalc.Controllers
{
    public class HomeController : Controller
    {

        public static List<string> History = new List<string>();

        public ActionResult Calc(string arg1, string arg2, string math)
        {
            decimal a = 0;
            decimal b = 0;
            string result = "";

            if (!decimal.TryParse(arg1, out a) || !decimal.TryParse(arg2, out b))
                result = "enter the arguments";

            switch (math)
            {
                case "+":
                    result = "Result:"+(a + b).ToString();
                    break;
                case "-":
                    result = "Result:"+(a - b).ToString();
                    break;
                case "*":
                    result = "Result:"+(a * b).ToString();
                    break;
                case "/":
                    result = b != 0 ?
                    (a / b).ToString()
                    : "error";
                    break;
                default:
                    result = "enter the arguments";
                    break;
            }
            History.Add(result);
            foreach (string r in History)
            {
                result = r;
            }
            return View(model: result);
        }
    }
}

Here is the view
@{
    ViewBag.Title = "Calc";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<form action="@Url.Action("Calc", "Home")">
    arg1</nav><input type="text" id="arg1" name="arg1" /><br /><br />
    arg2<input type="text" id="arg2" name="arg2" /><br />
    <output>@Model</output><br />
    <input type="radio" name="math" value="+" />+<br />
    <input type="radio" name="math" value="-" />-<br />
    <input type="radio" name="math" value="*" />*<br />
    <input type="radio" name="math" value="/" />/<br />
    <input type="submit" value="=" />
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Satisfied IT, 2018-06-21
@nordwind2013

If you get all the values ​​here

foreach (string r in History)
            {
                result = r;
            }

then fix result = r; for example, on result+= r;, since now a new value is entered into result each time, overwriting the previous one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question