在網站開發中我們經常需要用到表單,那么,在前台頁面的表單中提交到后台控制器后,后台控制器如何接收表單提交過來的數據呢?下面我們介紹幾種常用的方法。
我們先看看前台頁面,這里我們用一個用戶名和密碼的表單來作為前台頁面。
首先,我們新建一個MVC項目,然后添加一個控制器,UserInfoController;在控制器的默認方法index中,我們添加一個視圖。這個index視圖用來顯示我們的前台注冊頁面。
視圖如下:即使一個簡單的表單~
代碼如下,視圖的關鍵點就是把表單內容提交到哪個控制器的那個方法。也即是通過action的url啦處理。
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <!--提交到后台控制器中的GetUserInfo方法中--> <form action="~/UserInfo/GetUserInfo" method="post"> <table> <tr> <!--必須給每一個字段取一個唯一的name,后台控制器通過name來識別--> <td> 用戶名:<input type="text" name="username" /> </td> </tr> <tr> <td> 密 碼:<input type="text" name="password" /> </td> </tr> <tr> <td> <input type="submit" value="提交" /> </td> </tr> </table> </form> </div> </body> </html>
接下來我們就需要在后台控制器中處理表單提交過來的信息了。我們先在UserInfo控制器下再寫一個方法,用來接收表單傳過來的數據。
第一種方法,關鍵點在於參數名稱必須和表單的name是一致的。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcDemo.Controllers { public class UserInfoController : Controller { // GET: UserInfo public ActionResult Index() { return View(); } //參數的名稱需要和表單的字段名稱一致,這樣系統便會直接賦值。 public ActionResult GetUserInfo(string username,string password) { //為了方便演示,我們直接輸出這兩個值,表示我們已經拿到了數據 return Content(username+"*****"+password); } } }
第二種方法,FormCollection包含了表單的所有值,其實就是鍵值對,鍵就是表單字段中的name
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcDemo.Controllers { public class UserInfoController : Controller { // GET: UserInfo public ActionResult Index() { return View(); } //FormCollection包含了表單的所有值,其實就是鍵值對,鍵就是表單字段中的name public ActionResult GetUserInfo(FormCollection collection) { string username = collection["username"]; string password = collection["password"]; //為了方便演示,我們直接輸出這兩個值,表示我們已經拿到了數據 return Content(username+"*****"+password); } } }
第三種方法,直接拿值。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcDemo.Controllers { public class UserInfoController : Controller { // GET: UserInfo public ActionResult Index() { return View(); } public ActionResult GetUserInfo() { string username = Request["username"]; string password = Request["password"]; //為了方便演示,我們直接輸出這兩個值,表示我們已經拿到了數據 return Content(username+"*****"+password); } } }
第四種,通過建立一個對象來接受字段信息。只要對象的屬性和name對應,系統便會自動賦值。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcDemo.Controllers { public class UserInfoController : Controller { // GET: UserInfo public ActionResult Index() { return View(); } public ActionResult GetUserInfo(User user) { string username = user.Username; string password = user.Password; //為了方便演示,我們直接輸出這兩個值,表示我們已經拿到了數據 return Content(username+"*****"+password); } } public class User { private string username; public string Username { get { return username; } set { username = value; } } private string password; public string Password { get { return password; } set { password = value; } } } }