MVC增删改查例子

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

三、用属性方式显示用户列表
1.在UserInfoController控制器下添加Show方法

public ActionResult Show()
{
    DataTable table = SQLHelper.ExecuteReader("select Id, UserName, UserPwd from T_UserInfo");
    List<UserInfo> list = new List<UserInfo>();
    foreach (DataRow row in table.Rows)
    {
        UserInfo userinfo = new UserInfo();
        userinfo.Id = (int)row["Id"];
        userinfo.UserName = (string)row["UserName"];
        userinfo.UserPwd = (string)row["UserPwd"];
        list.Add(userinfo);
    }
    ViewData["UserInfo"] = list;
    return View();
}

2.在Show方法下新建Show视图

@using MVCText.Models;
@{
    List<UserInfo> user = ViewData["UserInfo"] as List<UserInfo>;
}
<table border="1" style="border-spacing:0px">
    <thead><tr><th>用户</th><th>密码</th></tr></thead>
    @{
        foreach (UserInfo row in user)
        {
            <tr>
                <td>@row.UserName</td>
                <td>@row.UserPwd</td>
            </tr>
        }    
    }
</table>

四、cshtml页面中获取强类型数据并显示数据库内容
1.在UserInfoController控制器下添加Show方法

public ActionResult Show()
{
    UserInfo userinfo = new UserInfo();
    userinfo.Id = 9;
    userinfo.UserName = "张三";
    userinfo.UserPwd = "7067";
    ViewData.Model = userinfo;
    return View();
}

2.在Show方法下新建Show视图
1)创建类获取
@{
  UserInfo user = ViewData.Model;
}
<h1>@user.Id</h1>
<h2>@user.UserName</h2>
<h3>@user.UserPwd</h3>
2)直接获取Model数据
<h1>@Model.UserName</h1>
<h2>@Model.UserPwd</h2>

五、删除用户

1.在UserInfoController控制器下添加Delete方法

public ActionResult Delete()
{
    int id = int.Parse(Request["Id"] ?? "0");//如果??左边的值Request["Id"]不为null则直接转,否则将0转为数字
    int r = SQLHelper.ExecuteNonQuery("delete from T_UserInfo where Id=@id",
        new SqlParameter { ParameterName = "id", Value = id });
    if (r > 0)
    {
        return RedirectToAction("Show");
    }
    else
    {
        return Content("删除失败!!!");
    }
}

2.在Show方法下新建Show视图

@{
    List<UserInfo> user = ViewData["UserInfo"] as List<UserInfo>;
}
<table border="1" style="border-spacing:0px">
    <thead><tr><th>用户</th><th>密码</th><th>删除</th></tr></thead>
    @{
        foreach (UserInfo row in user)
        {
            <tr>
                <td>@row.UserName</td>
                <td>@row.UserPwd</td>
                <td><a href="/UserInfo/Delete?Id=@row.Id" style="color:blue">删除</a></td>
            </tr>
        }    
    }
</table>

六、修改用户
1.在UserInfoController控制器下添加两个Edit方法并加上HttpGet和HttpPost特性

[HttpGet]
public ActionResult Edit(int Id)
{
    DataTable table = SQLHelper.ExecuteReader("select Id,UserName,UserPwd from T_UserInfo where Id=@Id",
        new SqlParameter("@id", Id));
    UserInfo user = new UserInfo();
    user.Id = Convert.ToInt32(table.Rows[0]["Id"]);
    user.UserName = (string)table.Rows[0]["UserName"];
    user.UserPwd = (string)table.Rows[0]["UserPwd"];
    ViewData["UserInfo"] = user;
    return View();
}

[HttpPost]
public ActionResult Edit(int Id, string UserName, string UserPwd)
{
    SQLHelper.ExecuteNonQuery("update T_UserInfo set UserName=@UserName,UserPwd=@UserPwd where Id=@Id",
        new SqlParameter("@UserName", UserName),
        new SqlParameter("@UserPwd", UserPwd),
        new SqlParameter("@id", Id));
    return RedirectToAction("Show");
}

2.在Show方法下新建Show视图

@{
    List<UserInfo> user = ViewData["UserInfo"] as List<UserInfo>;
}
<table border="1" style="border-spacing:0px">
    <thead><tr><th>用户</th><th>密码</th><th>编辑</th><th>删除</th></tr></thead>
    @{
        foreach (UserInfo row in user)
        {
            <tr>
                <td>@row.UserName</td>
                <td>@row.UserPwd</td>
                <td><a href="/UserInfo/Edit?Id=@row.Id" style="color:blue">编辑</a></td>
                <td><a href="/UserInfo/Delete?Id=@row.Id" style="color:blue">删除</a></td>
            </tr>
        }    
    }
</table>

3.在Edit方法下新建Edit视图

@using MVCText.Models;
@{
    UserInfo user = ViewData["UserInfo"] as UserInfo;    
}
<form action="/UserInfo/Edit?Id=@user.Id" method="post">
    <table>
        <tr>
            <td>用户</td><td><input type="text" name="UserName" value="@user.UserName" /></td>
        </tr>
        <tr>
            <td>密码</td><td><input type="password" name="UserPwd" value="@user.UserPwd" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="保存" /></td>
        </tr>
    </table>
</form>
原文地址:https://www.cnblogs.com/genesis/p/4838151.html