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

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; }
        }



    }
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值