I have a very simple form with 3 textbox and a button. I would like to get the value of the first two textbox by clicking the button and move to a method present in my model and after that return that result to the third textbox . I'm having difficulty passing the values and returning using the controller. I am a beginner in Asp.net mvc, if anyone can help. Thank you.
I have this Model method I want to use:
public class Conta
{
[Required]
public int Num1 { get; set; }
[Required]
public int Num2 { get; set; }
public double Result { get; set; }
public int Somar(int num1, int num2)
{
return num1 + num2;
}
That's no controller:
public ActionResult Index()
{
return View();
}
private static Conta _conta = new Conta();
[HttpPost]
public ActionResult Somar(Conta conta)
{
_conta.Somar(conta.Num1, conta.Num2);
return View();
}
And this in the index:
<tr>
<td>@Html.TextBoxFor(ContaModel => ContaModel.Num1)</td>
</tr>
<tr>
<td>@Html.TextBoxFor(ContaModel => ContaModel.Num2)</td>
</tr>
<tr>
<td><input type ="submit" value="Somar" name="Somar"/></td>
</tr>
</table>
As a beginner, I do not know if this is the right way to do it.