一、显示用户列表
1.新建UserInfoController控制器
public ActionResult Index() { DataTable table = SQLHelper.ExecuteReader("select UserName, UserPwd from T_UserInfo"); ViewData["table"] = table; return View(); }
2.新建视图Index
@using System.Data; @{DataTable table = (DataTable)ViewData["table"];} <table border="1" style="border-spacing:0px"> <thead><tr><th>用户</th><th>密码</th></tr></thead> @foreach (DataRow row in table.Rows) { <tr> <td>@row["UserName"]</td> <td>@row["UserPwd"]</td> </tr> } </table>
二、用户注册
1.在UserInfoController控制器下添加Add方法
public ActionResult Add() { return View(); }
2.在Add方法下新建Add视图
<form action="/UserInfo/ProcessAdd" method="post"> <table> <tr> <td>用户</td><td><input type="text" name="UserName" /></td> </tr> <tr> <td>密码</td><td><input type="password" name="UserPwd" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="注册用户" /></td> </tr> </table> </form>
3.在UserInfoController控制器下添加ProcessAdd方法
public ActionResult ProcessAdd() { string UserName = Request["UserName"]; //int UserPwd = !string.IsNullOrEmpty(Request["UserPwd"]) ? int.Parse(Request["UserPwd"]) : 0; string UserPwd = Request["UserPwd"]; int r = SQLHelper.ExecuteNonQuery("insert into T_UserInfo(UserName,UserPwd) values(@UserName,@UserPwd)", new SqlParameter { ParameterName = "@UserName", Value = UserName }, new SqlParameter { ParameterName = "@UserPwd", Value = UserPwd }); if (r <= 0) { return Content("注册失败!!!"); } return RedirectToAction("Index"); }
原文:http://www.cnblogs.com/genesis/p/4838151.html