Вы находитесь на странице: 1из 2

Create a web calculator using ASP.

NET core MVC to perform the


fundamental operations such as ADD, Subtract, Multiply, Divide, Remainder,
and Quotient by using buttons to perform each of the operations and display
the result of the operation on the same page

View
@{
ViewData["Title"] = "Home Page";
}
<form asp-action="index">
<div class="text-center">
<h1 class="display-4">Welcome to Calculator</h1>
<p><input type="number" placeholder="Enter First Value"name="firstvalue"
required></p>
<input type="number" placeholder="Enter Second Value"name="Secondvalue"
required></p>
<input type="submit" name="Cal" value="Add" class="btn btn-primary" />
<input type="submit" name="Cal" value="Sub" class="btn btn-primary" />
<input type="submit" name="Cal" value="Mul" class="btn btn-primary" />
<input type="submit" name="Cal" value="Div" class="btn btn-primary" />
<input type="submit" name="Cal" value="Remainder" class="btn btn-primary" />

</p>
Result<input type="number" value="@ViewBag.result" />
</p>

</div>
</form>

CONTROLLER

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

public HomeController(ILogger<HomeController> logger)


{
_logger = logger;
}

public IActionResult Index()


{
return View();
}
[HttpPost]
public IActionResult index(string firstvalue, string Secondvalue, String cal)
{
int a = Convert.ToInt32(firstvalue);
int b = Convert.ToInt32(Secondvalue);
int c = 0;
switch (cal)
{
case "Add":
c = a + b;
break;
case "Sub":
c = a - b;
break;
case "Mul":
c = a * b;
break;
case "Div":
c = a / b;
break;
case "Remainder":
c = a % b;
break;
}
ViewBag.Result = c;
return View();
}

public IActionResult Privacy()


{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore =


true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ??
HttpContext.TraceIdentifier });
}
}
}

Вам также может понравиться