用户管理实例 之 添加、查询

访问视图先是访问控制器中的方法,在去找跟方法同名的视图,或者指定的视图

这里是用LinQ查询数据库的一张表的数据,在控制器中ViewData存储数据,把此数据(一张表)在前台页面上显示出来。

控制器中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;   //LinQ to  SQL  类在Model中,要在这里拿数据

namespace MVCDemo.Controllers
{
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/
        NorthwindDBDataContext dc = new NorthwindDBDataContext();
        /// <summary>
        /// 显示数据的视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["data"] = dc.Users.AsEnumerable<Users>();
            return View();
        }
        /// <summary>
        /// 添加用户的试图
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 添加的具体方法
        /// </summary>
        /// <returns></returns>
        public ActionResult ProcessAdd()
        {
            //通过表单post获取值
            string name = Request.Form["name"].ToString();
            string pwd = Request.Form["pwd"].ToString();
            //string sex = Request["sex"];
            bool sex =Request.Form["sex"]=="on"?true:false;  //复选框控件,勾了显示   On
            Users user = new Users() { UserName = name, UserPwd = pwd, Sex = sex }; //实例一个用户
            dc.Users.InsertOnSubmit(user); //添加数据库中
            dc.SubmitChanges();  //更新数据库
            return RedirectToAction("Index");  //返回到指定试图
        }
    }
}

视图:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Import Namespace="MVCDemo.Models" %>   <%--引用命名空间--%>
<!DOCTYPE html> 

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        .auto-style1 {
             400px;
            background-color: #FFFF00;
        }
       table tr{
           background-color:#fff;
       }
    </style>
</head>
<body>
    <% IEnumerable<Users> list = ViewData["data"] as IEnumerable<Users>;  //把object转成可以被遍历的集合 %>  <%--写后台代码--%>
    <div>
        <table class="auto-style1">
            <tr>
                <td>ID</td>
                <td>UserName</td>
                <td>Pwd</td>
                <td>Sex</td>
            </tr>
            <% foreach (Users item in list)
               {
            %>
            <tr>
                <td><%=item.ID %></td>     <%--绑定数据--%>
                <td><%=item.UserName %></td>
                <td><%=item.UserPwd %></td>
                <td><%=item.Sex %></td>
            </tr>
            <%
               } %>
        </table>

    </div>
</body>
</html>

添加用户信息页面:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <style type="text/css">
        .auto-style1 {
            400px;
        }
    </style>
</head>
<body>
    <form action="/UserInfo/ProcessAdd" method="post">   //点提交后 去到控制器为UseInfo下的ProcessAdd 方法
        <table class="auto-style1">
            <tr>
                <td>名称:</td>
                <td>
                    <input type="text" name="name"  /></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td>
                    <input type="text" name="pwd" /></td>
            </tr>
              <tr>
                <td>性别:</td>
                <td>
                    <input type="checkbox" name="sex"  />男  </td>
            </tr>
               <tr >
                <td colspan="2">
                    <input type="submit"  value="添加用户" /></td>               
            </tr>
        </table>
    </form>
</body>
</html>
原文地址:https://www.cnblogs.com/Sea1ee/p/5958100.html