MVC的多表单
中心思想就是在一个表单内不规定”action“,在js里面用@Url.Axtion("视图层","控制器")方法来设置表单的传值。
控制器
public ActionResult Jia(string a1, string a2) { int end = Convert.ToInt32(a1) + Convert.ToInt32(a2); TempData["a1"] = a1; TempData["a2"] = a2; TempData["Jia"] = end; return RedirectToAction("Other1","Home"); } public ActionResult Jian(string a1, string a2) { int end = Convert.ToInt32(a1) - Convert.ToInt32(a2); TempData["a1"] = a1; TempData["a2"] = a2; TempData["Jia"] = end; return RedirectToAction("Other1", "Home"); }
视图层
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Other1</title> <script src="~/js/jquery-1.7.2.min.js"></script> </head> <body> <div> <form method="post"> <input type="text" name="a1" value="@TempData["a1"]" /> <input type="text" name="a2" value="@TempData["a2"]" />= <input type="text" name="a3" value="@TempData["Jia"]" /> <input type="button" id="btn_jia" value="加法计算" /> <input type="button" id="btn_jian" value="减法计算" /> </form> <script type="text/javascript"> document.getElementById("btn_jia").onclick = function () { this.form.setAttribute("action", "@Url.Action("Jia", "Home")"); this.form.submit(); }; document.getElementById("btn_jian").onclick = function () { this.form.setAttribute("action", "@Url.Action("Jian", "Home")"); this.form.submit(); }; </script> </div> </body> </html>
完!!