ASP.NET在MVC控制器中擷取Form表單值的方法

來源:互聯網
上載者:User

標籤:html   name   user   idt   method   http   img   前台   layout   

在網站開發中我們經常需要用到表單,那麼,在前台頁面的表單中提交到後台控制器後,後台控制器如何接收表單提交過來的資料呢?下面我們介紹幾種常用的方法。

我們先看看前台頁面,這裡我們用一個使用者名稱和密碼的表單來作為前台頁面。

首先,我們建立一個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; }        }    }}

 

ASP.NET在MVC控制器中擷取Form表單值的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.