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; } //密码 }
视图代码如下:
@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>
控制器接受数据方式:
1.Request.Form
public ActionResult UserEdit() { UserModel model = new UserModel(); model.UserName = Request.Form["UserName"]; model.Password = Request.Form["Password"]; return View(model ); }
2.FormCollection
public ActionResult UserEdit(FormCollection form) { UserModel model = new UserModel(); model.UserName = form["UserName"]; model.Password =form["Password"]; return View(model); }
3.对象读取
public ActionResult UserEdit(UserModel userModel) { Response.Write(userModel.UserName); Response.Write("<br />"); Response.Write(userModel.Password); return View(); }
时间: 2024-11-10 04:33:37