码迷,mamicode.com
首页 > Web开发 > 详细

[asp.net mvc] 将视图中的表单数据传递到控制器中

时间:2017-02-06 14:25:46      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:log   pre   数据   set   first   submit   bsp   blog   字段   

在ASP.NET MVC框架中,将视图中的数据传递到控制器中,主要通过发送表单实现的。具体使用中,主要使用以下三种方法。

 

1.通过Request.Form读取表单数据

表单代码:

1 @using (Html.BeginForm("Person", "Default3"))
2 {
3     @Html.TextBox("tFirstName")
4     <br />
5     @Html.TextBox("tLastName")
6     <br />
7     <input type="submit" value="提交" /> 
8 }

 

使用Request.Form提取表单数据:

1         [HttpPost]
2         public JsonResult Person()
3         {
4             JsonResult result = new JsonResult();
5             result.Data = new object[] { Request.Form["FirstName"], Request.Form["LastName"] };
6             return result;
7         }

 

2.通过FormCollection读取表单数据

1         [HttpPost]
2         public JsonResult Person(FormCollection form)
3         {
4             JsonResult result = new JsonResult();
5             result.Data = new object[] { form["FirstName"], form["LastName"] };
6             return result;
7         }

 

3、直接读取表单数据对象

首先定义一个Person类如下:

1     public class Person
2     {
3         public string FirstName { get; set; }
4         public string LastName { get; set; }
5     }

Person()的实现:

1         [HttpPost]
2         public JsonResult Person(Person person)
3         {
4             JsonResult result = new JsonResult();
5             result.Data = new object[] { person.FirstName, person.LastName };
6             return result;
7         }

Person类的字段名必须和表单中input的name一致。

或者:

1         [HttpPost]
2         public JsonResult Person(string FirstName, string LastName)
3         {
4             JsonResult result = new JsonResult();
5             result.Data = new object[] { FirstName, LastName };
6             return result;
7         }

参数名必须和表单中input的name一致。

[asp.net mvc] 将视图中的表单数据传递到控制器中

标签:log   pre   数据   set   first   submit   bsp   blog   字段   

原文地址:http://www.cnblogs.com/jinxin201211/p/6369980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!