首页 > Web开发 > 详细

MVC例子

时间:2015-09-25 16:05:25      阅读:177      评论:0      收藏:0      [点我收藏+]

一、显示用户列表
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");
}

 

MVC例子

原文:http://www.cnblogs.com/genesis/p/4838151.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!