Essentially, I need to create a web application that has the user input three integers, and then prints out their sum. I need to create this program in a class and print it using the HTTP Post protocol.
Below are the Console app I made first, as well as my attempt at doing this in a standard MVC (minus the Model). My particular issue is, I have very little experience with the Post method outside of HTML forms, and trying to look up information on it hasn't helped.
1st Attempt - Console App
int num1;
int num2;
int num3;
int sum;
Console.Write("First number: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Third number: ");
num3 = Convert.ToInt32(Console.ReadLine());
sum = num1 + num2 + num3;
Console.WriteLine("Sum: {0}", sum);
Console.ReadLine();
2nd Attempt - Controller
public class HomeController : Controller
{
// GET: Home
// Integers initiated in the index parameters.
public ActionResult Index(int firstInt = -0, int secondInt = -0, int thirdInt = -0)
{
// Calculates sum of the three integers, and then sends result to the View.
int sum = firstInt + secondInt + thirdInt;
ViewBag.result = sum;
return View();
}
}
2nd Attempt - Index
@{
ViewBag.Title = "Index";
}
<form action="" method="post">
<table>
<tr><td>Enter the 1st Number: <input id="firstInt" name="firstInt" type="text" value="0" /></td></tr>
<tr><td>Enter the 2nd Number: <input id="secondInt" name="secondInt" type="text" value="0" /></td></tr>
<tr><td>Enter the 3rd Number: <input id="thirdInt" name="thirdInt" type="text" value="0" /></td></tr>
<tr>
<td><input id="Submit" type="submit" value="submit" /><input id="Reset" type="reset" value="reset" /></td>
</tr>
<tr>
<td>
Sum =
@if (ViewBag.result != 0)
{
@ViewBag.result
}
</td>
</tr>
</table>