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; }
} }
}
ASP.NET在MVC控制器中获取Form表单值的方法的更多相关文章
- ASP.Net MVC 在控制器中获取View中的form表单值的方法
		在网站开发中,我们常常需要用到表单提交的方式,那么在MVC中是如何获取到表单中的数据呢?下面我们来介绍以下几种方式 首先我们先看看View前台页面 添加一个控制器 我们再看看前台页面的代码 @{ La ... 
- ASP.Net 获取Form表单值
		新建一HtmlPage1.html,如下post发送() <body> <form enctype="multipart/form-data" action=&q ... 
- 在ASP.NET MVC控制器中获取链接中的路由数据
		在ASP.NET MVC中,在链接中附加路由数据有2种方式.一种是把路由数据放在匿名对象中传递: <a href="@Url.Action("GetRouteData&quo ... 
- 在 easyui中获取form表单中所有提交的数据 拼接到table列表中
		form表单===== <!-- 并用药品填写信息弹框 --> <div id="usingProdctMsgDiv" style="display: ... 
- 过滤器中获取form表单或url请求数据
		var httpFormData = filterContext.HttpContext.Request.Form; var logContent = string.Empty; //获取url的 l ... 
- MVC 在控制器中获取某个视图动态的HTML代码
		ASP.NET MVC 在控制器中获取某个视图动态的HTML代码 如果我们需要动态的用AJAX从服务器端获取HTML代码,拼接字符串是一种不好的方式,所以我们将HTML代码写在cshtml文件中, ... 
- MVC教程二:从控制器中获取URL的值
		一.从控制器中获取URL的值有三种方式: 1.使用Request.QueryString[] 例如: string value = Request.QueryString["BookId&q ... 
- ASP.NET Core 入门教程 4、ASP.NET Core MVC控制器入门
		一.前言 1.本教程主要内容 ASP.NET Core MVC控制器简介 ASP.NET Core MVC控制器操作简介 ASP.NET Core MVC控制器操作简介返回类型简介 ASP.NET C ... 
- ASP.NET Core 入门笔记5,ASP.NET Core MVC控制器入门
		摘抄自https://www.cnblogs.com/ken-io/p/aspnet-core-tutorial-mvc-controller-action.html 一.前言 1.本教程主要内容 A ... 
随机推荐
- [HEOI/TJOI2016]序列
			Description: 给你一个序列,每个数可能变化为另一个数,每次最多有一个数变化 求最长的子序列,无论如何变化,这个子序列都不下降 Hint: \(n \le 10^5\) Solution: ... 
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8
			#include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ... 
- MyBatis(七) 自定义映射结果ResultMap
			(1)接口中对应的方法 public Emp getEmpById(Integer id); (2)Mapper文件 <resultMap type="com.eu.bean.Emp& ... 
- js 原生转json 可以v8中运行
			// load("D:/jsontest.js"); function test1(vvv) { print(vvv); } //把json str 转 json obj func ... 
- Java实现生产者与消费者模式
			生产者不断向队列中添加数据,消费者不断从队列中获取数据.如果队列满了,则生产者不能添加数据:如果队列为空,则消费者不能获取数据.借助实现了BlockingQueue接口的LinkedBlockingQ ... 
- javascript js原生ajax post请求 实例
			HTML代码: 注意: xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencod ... 
- 服务器http://localhost:8080要求用户输入用户名和密码
			我们在将web项目部署运行的时候,想要在浏览器上输入http://localhost:8080时却提示: 如果你的电脑安装过Oracle的话,可能是和Oracle 的端口一样了,这是可以有两个办法解决 ... 
- Spring Cloud 组件 —— feign
			feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ... 
- css知识总结
			---# 学习目标:> 1. 学会使用CSS选择器> 2. 熟记CSS样式和外观属性> 3. 熟练掌握CSS各种选择器> 4. 熟练掌握CSS各种选择器> 5. 熟练掌握 ... 
- MySql解除安全模式:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
			在修改一条数据字段时报错: Error Code: 1175. You are using safe update mode and you tried to update a table witho ... 
