title: ASP.NET MVC Form - What is the proper way to return to a view with the previous form values?
body: <p>I had an assignment to implement a WCF Service and call it from a client of any type. I already have some experience with console, winform, WPF apps, so I wanted to use this as a chance to learn ASP.NET MVC. </p>
  
  <p>I know that I'm not properly implementing the "M" part of it yet, but I'm wondering what the correct way of processing data on a form in order to update show a result would be instead of what I have (working) so far:</p>
  
  <p>View:</p>
  
  <pre><code>@using (Html.BeginForm("UseSimpleMathClient", "Home"))
  {
      &lt;div style="width:450px"&gt;
          &lt;table style="width:100%"&gt;
              &lt;tr&gt;&lt;td&gt;&lt;h4&gt;@Html.Label("First number", new { @for = "num1" })&lt;/h4&gt;&lt;/td&gt; &lt;td&gt; @Html.TextBox("num1", ViewData["num1"])&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;&lt;td&gt;&lt;h4&gt;@Html.Label("Second number", new { @for = "num2" })&lt;/h4&gt;&lt;/td&gt; &lt;td&gt; @Html.TextBox("num2", ViewData["num2"])&lt;/td&gt;&lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td colspan="2"&gt;
                      &lt;h2 style="text-align:center"&gt;
                          &lt;input type="submit" name="operation" value="+" /&gt;
                          &lt;input type="submit" name="operation" value="-" /&gt;
                          &lt;input type="submit" name="operation" value="*" /&gt;
                          &lt;input type="submit" name="operation" value="÷" /&gt;
                      &lt;/h2&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td colspan="2"&gt;&lt;b&gt;@Html.Label("Result")&lt;/b&gt; &amp;nbsp; @Html.TextBox("result", ViewData["result"], new {disabled = "disabled", style="min-width:80%;text-align:center" })&lt;/td&gt;
              &lt;/tr&gt;
              &lt;/table&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>      public ActionResult USeSimpleMathClient(char operation)
      {
          float num1, num2;
          float? result = null;
  
          if (!float.TryParse(Request.Form[0], out num1) || !float.TryParse(Request.Form[1], out num2))
          {
              ViewData["result"] = "Please enter valid numbers before selecting an operation.";
          }
          else
          {
              switch (operation)
              {
                  case '+':
                      result = mathClient.Add(num1, num2);
                      break;
                  case '-':
                      result = mathClient.Subtract(num1, num2);
                      break;
                  case '*':
                      result = mathClient.Multiply(num1, num2);
                      break;
                  case '÷':
                      if (num2 != 0)
                          result = mathClient.Divide(num1, num2);
                      break;
                  default:
                      break;
              }
  
              ViewData["result"] = result != null ? String.Format("{0} {1} {2} = {3}", num1, operation, num2, result) : "You can't divide by zero!";
          }
  
          ViewData["num1"] = Request.Form[0];
          ViewData["num2"] = Request.Form[1];
          return View("Index");
      }
  </code></pre>