I am very new to mvc.I have single textbox. when buttons are clicked button value are displayed in textbox. What i want is to add values using controller.
Here is my view:
<html>
<head>
<title>
</title>
<script>
window.onload = function() {
document.getElementById('btnone').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnone').value;
});
document.getElementById('btntwo').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btntwo').value;
});
document.getElementById('btnthree').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnthree').value;
});
document.getElementById('btnfour').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnfour').value;
});
document.getElementById('btnadd').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnadd').value;
});
document.getElementById('btneql').addEventListener('click', function(){
});
}
</script>
</head>
<body>
<input type="button" name="btnone" id="btnone" value="1" onclick="setText1()" />
<input type="button" name="btntwo" id="btntwo" value="2" onclick="setText2()" />
<input type="button" name="btnthree" id="btnthree" value="3" onclick="setText3()" />
<input type="button" name="btnfour" id="btnfour" value="4" onclick="setText4()" />
<br/>
<input type="button" name="btnadd" id="btnadd" value="+" onclick="setText+()" />
<input type="button" name="btneql" id="btneql" value="=" onclick="setText=()" />
<input type="text" name="textBox" id="textBox" value=""/>
</body>
</html>
here is my model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
namespace MvcApplicationDemo.Models
{
public class Calci
{
[Required]
[DataType(DataType.Text)]
[Display(Name = "No1")]
[Compare("0", ErrorMessage = "No1 must not be 0")]
public string Noone { get; set; }
}
public class CalculatorModel
{
[Required]
[DataType(DataType.Text)]
[Display(Name = "No1")]
public string firstOperand { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "No2")]
public int secondOperand { get; set; }
public string button { get; set; }
public string textBox { get; set; }
public int result { get; set; }
Here is my controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationDemo.Models;
namespace MvcApplicationDemo.Controllers
{
public class demo1Controller : Controller
{
public ActionResult Demo1()
{
CalculatorModel model = new CalculatorModel();
return View(model);
}
[HttpGet]
public ActionResult Sum()
{
CalculatorModel model = new CalculatorModel();
return View(model);
}
[HttpPost]
public ActionResult Demo1(string button, CalculatorModel model)
{
return View(model);
}
}
}