MVC 從后台頁面 取前台頁面傳遞過來的值的幾種取法


 
 

<1>前台頁面 Index視圖

注意:用戶名表單的name值為txtName

           密碼表單的name值為txtPassword

 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>后台頁面,Home控制器 (為了測試,分別將視圖頁中的from表單的action設為 action="/Home/Test" ,action="/Home/Test2" action="/Home/Test3" action="/Home/Test4" )

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MvcApplication1.Controllers
 8 {
 9     public class HomeController : Controller
10     {
11         //
12         // GET: /Home/
13 
14         public ActionResult Index()
15         {
16             return View();
17         }
18 
19         /// <summary>
20         /// MVC第一種取值方式
21         /// </summary>
22         /// <returns></returns>
23         public ActionResult Test()
24         {
25             string userName = Request["txtName"];   //此時Request["txtName"]=ABC
26             string password = Request["password"];  //此時Request["password"]=123
27 
28             return Content("OK" + userName + password);
29         }
30         /// <summary>
31         /// 第二種取值方式
32         /// </summary>
33         /// <param name="f"></param>
34         /// <returns></returns>
35         public ActionResult Test2(FormCollection f) //FormCollection是MVC中表單里的一個集合,它也可以來接收前台提交過來的表單,前台提交過來的表單全都封裝到這個對象中來了
36         {
37             string userName = f["txtName"];     //此時f["txtName"]=ABC 
38             string password = f["txtPassword"]; //此時f["txtPassword"]=123
39 
40             return Content("OK" + userName + password);
41         }
42         /// <summary>
43         /// 第三種取值方式
44         /// </summary>
45         /// <param name="txtName"></param>
46         /// <param name="txtPassword"></param>
47         /// <returns></returns>
48         public ActionResult Test3(string txtName, string txtPassword) //注意這個參數的名稱必須要與前台頁面控件的 name值是一致的
49         {
50             return Content("OK" + txtName + txtPassword);
51 
52             //此時textName=ABC 
53             //此時txtPassword=123
54         }
55 
56         /// <summary>
57         /// 第四中方式
58         /// </summary>
59         /// <param name="txtName"></param>
60         /// <param name="txtPassword"></param>
61         /// <param name="p"></param>
62         /// <returns></returns>
63         public ActionResult Test4(string txtName, string txtPassword, ParaClass p) //如果ParaClass類里面的屬性與前台頁面控件的name值一致,那么它的對象屬性也會自動被賦值
64         {
65             return Content("OK" + txtName + txtPassword + p.txtName + p.txtPassword);
66 
67             //此時textName=ABC 
68             //此時txtPassword=123
69 
70             //此時p.txtName=ABC
71             //此時p.txtPassword=123
72         }
73 
74 
75         public class ParaClass
76         {
77             public string txtName { get; set; } //此時textName=ABC
78             public string txtPassword { get; set; } //此時txtPassword=123
79         }
80       
81     }
82 }

 

 
           
          

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM