随笔 - 35,  文章 - 3,  评论 - 100,  阅读 - 19万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
 
 

<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 }
复制代码

 



 

 

posted on   Jerry Tian  阅读(17430)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示