Use ajax to transmit and receive data, and use ajax to transmit and receive data
Front-end code:
1 <input id = "txtNum1" name = "txtNum1" type = "text" width = "130"/> 2 <select id = "fh" name = "fh"> 3 <option id = "jia" value = "+"> + </option> 4 <option id = "jian" value = "-">-</option> 5 <option id = "chen" value = "*"> * </option> 6 <option id = "chu" value = "/">/</option> 7 </select> 8 <input id = "txtNum2" name = "txtNum2" type = "text" width = "130"/> 9 <input id = "btnSubmit" type = "button" value = "="/> 10 <input id = "txtResult" type = "text" width = "130"/> 11 12 @ section scripts {13 <script> 14 $ (function () {15 $ ("# btnSubmit "). bind ("click", function () {16 debugger17 $. ajax ({18 @ * url: '@ Url. action ("JS") ', * @ 19 url: "/Home/JS", 20 data: {21 "num1": $ ("# txtNum1 "). val (), 22 "num2": $ ("# txtNum2 "). val (), 23 "fh": $ ("# fh "). val () 24}, 25 dataType: "json", 26 type: "post", 27 success: function (data) {28 $ ("# txtResult "). val (data. result); 29}, 30 error: function (e, a) {31 alert (a); 32} 33}) 34}) 35}) 36 </script> 37}View Code
Background action code:
1 [HttpPost] 2 public JsonResult JS (string num1, string num2, string fh) 3 {4 int result = 0; 5 try 6 {7 switch (fh) 8 {9 case "+": 10 result = Convert. toInt32 (num1) + Convert. toInt32 (num2); break; 11 case "-": 12 result = Convert. toInt32 (num1)-Convert. toInt32 (num2); break; 13 case "*": 14 result = Convert. toInt32 (num1) * Convert. toInt32 (num2); break; 15 case "/": 16 result = Convert. toInt32 (num1)/Convert. toInt32 (num2); break; 17 default: 18 break; 19} 20} 21 catch (Exception ex) 22 {23 log. error ("Error", new Exception (ex. message); 24 return Json (""); 25} 26 var data = new {Result = result}; 27 return Json (data); 28}View Code