-1

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>

D. Huber
  • 7
  • 4
  • What do you mean by "print the result using the HTTP Post protocol"? Do you want the console App to print the result in a HTTP response? – Kzryzstof Oct 11 '17 at 13:23
  • You have to add the controller action to the form. See https://stackoverflow.com/questions/15190929/how-to-link-html5-form-action-to-controller-actionresult-method-in-asp-net-mvc-4 – Greg Oct 11 '17 at 13:24

1 Answers1

0

You need two Index( ) methods, I believe. One takes no parameters and just returns View( ) to display the input form, the second one is given the [HttpPost] attribute and is largely as you have written it.

This is using ASP.NET MVC and Razor pages, and the routing engine will make sure the [HttpPost] method is selected for the POST request.

Kevmeister68
  • 216
  • 1
  • 2
  • 6
  • What do I need to write differently with Razor in order to have the form communicate with the method, and so that ASP can reconcile the two identically-named controllers? – D. Huber Oct 11 '17 at 16:53
  • I've explained it already. You have to write two Index( ) methods in the same controller class. One of these will have the [HttpPost] attribute, so you end up with eg. void Index( ) { } [HttpPost] void Index( some-arguments ) { } ASP.NET MVC will see an incoming request, which will be either a GET or POST HTTP verb [for the purpose of this example], and direct the request to either of the above controller methods depending on the HTTP verb it sees. – Kevmeister68 Nov 10 '17 at 23:41