I need a controller that on a click of a button will store on the model var1 the value of 1 or 2 depending on which radio button is selected, store on var2 the value of 3 or 4 depending on the option that is selected from the list and after that calculate var3 (var1 + var2) and write it on the raw of the column "result".
Here is the code:
Model (Class1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test1.Models
{
public class Class1
{
public int var1 { get; set; }
public int var2 { get; set; }
public int var3 { get; set; }
}
}
Controller (HomeController)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Test1.Controllers
{
public class HomeController : Controller
{
public ActionResult Page()
{
return View();
}
}
}
View (Page)
@model Test1.Models.Class1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Page</title>
</head>
<body>
<div>
@using (Html.BeginForm("Page", "Class1", FormMethod.Post)) //should I use this without the FormMethod.Post?
{
<table style="width:100%" align="center" cellpadding="10">
<tr>
<th>1</th>
<th>2</th>
<th>var2</th>
<th>Result</th>
</tr>
<tr>
<td> @Html.RadioButtonFor(m => m.var1, "1") </td>
<td> @Html.RadioButtonFor(m => m.var1, "2") </td>
<td> @Html.DropDownListFor(m => m.var2, new List<SelectListItem>
{new SelectListItem{Text="3", Value="3"},
new SelectListItem{Text="4", Value="4"}})
</td>
<td></td>
</tr>
</table>
}
<input id="Button1" type="button" value="button"/>
</div>
</body>
</html>