Asp.Net MVC控制器獲取檢視傳值幾種方式

我們要發財了發表於2020-12-28

前臺表單(V:檢視)

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>UserRegist</title>
11 </head>
12 <body>
13     <div> 
14         <form action="/UserInfo/ProcessUserRegist" method="post">
15             <table>
16                 <tr>
17                     <td>使用者名稱:</td><td><input type="text" name="UserId" /></td>
18                 </tr>
19                 <tr>
20                     <td>密碼:</td><td><input type="password" name="pwd" /></td>
21                 </tr>
22                 <tr>
23                     <td colspan="2">
24                         <input type="submit" value="提交"/>
25                         <input type="reset" value="重置"/>
26                     </td>
27                 </tr>
28             </table>
29         </form>
30     </div>
31 </body>
32 </html>在這裡插入程式碼片

方式一(C:控制器)

1         /// <summary>
 2         /// 方式一
 3         /// </summary>
 4         /// <returns></returns>
 5         public ActionResult ProcessUserRegist()
 6         {
 7             string userId = Request.QueryString["UserId"] ?? Request.Form["UserId"];
 8             string pwd = Request.QueryString["pwd"] ?? Request.Form["pwd"];
 9             return Content(userId + "註冊成功!"); //相當於 Response.Write("");Response.End();
10         }
方式二(C:控制器)

方式三(C:控制器)

 1         /// <summary>
 2         /// 方式三
 3         /// </summary>
 4         /// <param name="UserId"></param>
 5         /// <param name="pwd"></param>
 6         /// <returns></returns>
 7         public ActionResult ProcessUserRegist(string UserId,string pwd)
 8         {
 9             return Content(UserId + "註冊成功!"); //相當於 Response.Write("");Response.End();
10         }

方式四(C:控制器)

1         /// <summary>
 2         /// 方式四
 3         /// </summary>
 4         /// <param name="result"></param>
 5         /// <returns></returns>
 6         public ActionResult ProcessUserRegist(Result result)
 7         {
 8             return Content(result.UserId + "註冊成功!"); //相當於 Response.Write("");Response.End();
 9         }
10         public class Result
11         {
12             public string UserId { get; set; }
13             public string pwd { get; set; }
14         }

轉載於::https://www.cnblogs.com/chenyanbin/p/11259532.html

相關文章