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

MVC中接受视图传递数据(表单)的方法

时间:2015-07-26 15:41:40      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

1.通过Request.Form读取表单数据        2、通过FormCollection读取表单数据        3、通过对象读取表单数据

首先定义一个UserModel类:

技术分享
 public class UserModel
    {
         public int UserID { get; set; }                 //用户编号
        public string UserName { get; set; }            //用户名
        public string Password { get; set; }            //密码
    }
View Code

视图代码如下:

技术分享
@model MvcDemo.Models.UserModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>用户编辑</title>
</head>
<body>
    @using (@Html.BeginForm())
    {
        <div>
            用户名:@Html.TextBoxFor(model => model.UserName, new { @style = "width:200px" })
        </div>
        <div>
            密码:  @Html.PasswordFor(model=>model.Password)
        </div>
        <div>
            <input type="submit" value="提交" /></div>
    }
</body>
</html>
View Code

控制器接受数据方式:

1.Request.Form

技术分享
 public ActionResult UserEdit()
       {
           UserModel model = new UserModel();
           model.UserName = Request.Form["UserName"];
           model.Password = Request.Form["Password"];
           return View(model );
       }
View Code

2.FormCollection

技术分享
 public ActionResult UserEdit(FormCollection form)
       {
           UserModel model = new UserModel();
           model.UserName = form["UserName"];
           model.Password =form["Password"];
           return View(model);
       }
View Code

3.对象读取

技术分享
  public ActionResult UserEdit(UserModel userModel)
        {
            Response.Write(userModel.UserName);
            Response.Write("<br />");
            Response.Write(userModel.Password);
            return View();
        }
View Code

 

MVC中接受视图传递数据(表单)的方法

标签:

原文地址:http://www.cnblogs.com/spark123/p/4677608.html

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