If I have this in my View:
@using (Html.BeginForm("Sum","Student"))
{
<p>
Number1:
@Html.TextBox("num1")
</p>
<P>
Number2:
@Html.TextBox("num2")
</P>
<input type="submit" value="Calculate"/>
}
And this in my controller:
public void Sum(FormCollection values)
{
var num11 = Convert.ToInt32(values["num1"]);
var num21 = Convert.ToInt32(values["num2"]);
int result = num11 + num21;
//How return result back to view and show it in a textbox
}
How can I return the result back to the view and show it in a textbox
?
Edit: I tried the following code as suggested but it then redirect me to another view without any result. I want to show the result in the same view that I send the data to the sum method:
public void Sum(FormCollection values)
{
....
ViewBag.result = result;
}
And:
....
<input type="submit" value="Calculate"/>
string str = (string)ViewBag.Res;
@Html.TextBox("Result",str)