例:
表单提交数据,后台略作处理,前台显示。
控制器代码:
1 public class HomeController : Controller 2 { 3 // GET: Home 4 public ActionResult Index() 5 { 6 if (Request.QueryString["t"] != null) 7 { 8 ViewData["mydata"] = Request.QueryString["t"]; 9 } 10 else 11 { 12 ViewData["mydata"] ="0"; 13 } 14 return View(); 15 } 16 public void do_index(string a) 17 { 18 int t; 19 t=int.Parse(a); 20 t++; 21 Response.Redirect("/home/index?t=" + t.ToString()); 22 } 23 }
前台代码(index.cshtml):
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> @ViewData["mydata"].ToString() <form action="/home/do_index"> <input type="text" name="a" /><br /> <input type="submit" /> </form> </div> </body> </html>
测试通过