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

在网站开发中我们经常需要用到表单,那么,在前台页面的表单中提交到后台控制器后,后台控制器如何接收表单提交过来的数据呢?下面我们介绍几种常用的方法。

我们先看看前台页面,这里我们用一个用户名和密码的表单来作为前台页面。

首先,我们新建一个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; } } }}

 

相关文章