ASP.NET MVC 后台获取前台页面传值的几种方法?

<1>前台代码(调用后台中指定的Action需更将前台中的Url改为指定的Action名

 1 <html>  
 2 <head>  
 3     <meta name="viewport" content="width=device-width" />  
 4     <title>Test</title>  
 5 </head>  
 6 <body>  
 7     <form action="/Home/Test" method="post">  
 8         <div>  
 9             <label>用户名</label><input type="text" name="txtName" />  
10             <label>密 码</label><input type="text" name="txtPassword" />  
11         </div>  
12         <input type="submit" value="提交" />  
13     </form>  
14 </body>  
15 </html>  

<2>后台代码

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
  
namespace MvcApplication1.Controllers  
{  
    public class HomeController : Controller  
    {  
        //  
        // GET: /Home/  
  
        public ActionResult Index()  
        {  
            return View();  
        }  
  
  
        // MVC第一种取值方式  
  
        public ActionResult Test()  
        {  
            string userName = Request["txtName"];   //此时Request["txtName"]=ABC  
            string password = Request["password"];  //此时Request["password"]=123  
  
            return Content("OK" + userName + password);  
        }  
  
        // 第二种取值方式  
  
        public ActionResult Test2(FormCollection f) //FormCollection是MVC中表单里的一个集合,它也可以来接收前台提交过来的表单,前台提交过来的表单全都封装到这个对象中来了  
        {  
            string userName = f["txtName"];     //此时f["txtName"]=ABC   
            string password = f["txtPassword"]; //此时f["txtPassword"]=123  
  
            return Content("OK" + userName + password);  
        }  
  
        // 第三种取值方式  
  
        public ActionResult Test3(string txtName, string txtPassword) //注意这个参数的名称必须要与前台页面控件的 name值是一致的  
        {  
            return Content("OK" + txtName + txtPassword);  
  
            //此时textName=ABC   
            //此时txtPassword=123  
        }  
  
  
        // 第四种取值方式,通过类对象接收  
  
        public ActionResult Test4(ParaClass p) //如果ParaClass类里面的属性与前台页面控件的name值一致,那么它的对象属性也会自动被赋值  
        {  
            return Content("OK" + p.txtName + p.txtPassword);  
  
            //此时p.txtName=ABC  
            //此时p.txtPassword=123  
        }  
  
  
        public class ParaClass  
        {  
            public string txtName { get; set; } //此时textName=ABC  
            public string txtPassword { get; set; } //此时txtPassword=123  
  
            //注意:ParaClass类的属性名称必须要与前端页面控件的name值一致,这样才能接收到前端页面传递过来的值  
        }  
        
    }  
}