zoukankan      html  css  js  c++  java
  • MVC--数据增删改查(aspx语法)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Mvc1.Models;
    
    namespace Mvc1.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            //添加
            public ActionResult Insert()
            {
                return View();
            }
    
            public ActionResult Insert1(string username,string password,string nickname,bool gender,DateTime birthday,string nation,string area)
            {
                Users u = new Users();
                u.username = username;
                u.password = password;
                u.nickname = nickname;
                u.gender = gender;
                u.birthday = birthday;
                u.nation = nation;
                u.area = area;
    
                new UsersData().Insert(u);
    
                return RedirectToAction("Index", "Home");
            }
    
            //删除
            public ActionResult Delete(string id)
            {
                new UsersData().Delete(id);
                return RedirectToAction("Index", "Home");
            }
    
            //修改
            public ActionResult Update(string id)
            {
                Users u = new UsersData().Select1(id);
                ViewBag.data = u;
                return View();
            }
    
            public ActionResult Update1(Users u)
            {
                new UsersData().Update(u);
                return RedirectToAction("Index","Home");
            }
        }
    }
    Controllers
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvc1.Models
    {
        public class UsersData
        {
            DataClasses1DataContext conn = new DataClasses1DataContext();
    
            /// <summary>
            /// 查询全部数据
            /// </summary>
            /// <returns>List</returns>
            public List<Users> Select()
            {
                return conn.Users.ToList();
            }
    
            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="u">Users u</param>
            public void Insert(Users u)
            {
                conn.Users.InsertOnSubmit(u);
                conn.SubmitChanges();
            }
    
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="username">username</param>
            public void Delete(string username)
            {
                Users u = conn.Users.Where(r => r.username == username).FirstOrDefault();
                conn.Users.DeleteOnSubmit(u);
                conn.SubmitChanges();
            }
    
            /// <summary>
            /// 显示指定数据
            /// </summary>
            /// <param name="username">username</param>
            /// <returns>Users</returns>
            public Users Select1(string username)
            {
                return conn.Users.Where(r => r.username == username).FirstOrDefault();
            }
    
            /// <summary>
            /// 修改
            /// </summary>
            /// <param name="u"></param>
            public void Update(Users u)
            {
                Users uu = conn.Users.Where(r => r.username == u.username).FirstOrDefault();
    
                uu.password = u.password;
                uu.nickname = u.nickname;
                uu.birthday = u.birthday;
                uu.gender = u.gender;
                uu.nation = u.nation;
                uu.area = u.area;
    
                conn.SubmitChanges();
            }
        }
    }
    Models-UsersData
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ Import Namespace="Mvc1.Models" %>
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <table style="background-color:navy;100%;text-align:center;">
            <tr style="color:white;">
                <td>用户名</td>
                <td>密码</td>
                <td>昵称</td>
                <td>性别</td>
                <td>生日</td>
                <td>民族</td>
                <td>地区</td>
                <td>操作</td>
            </tr>
    
            <%
                List<Users> list = new UsersData().Select();
                foreach(Users u in list)
                {
            %>
    
            <tr style="background-color:white;">
                <td><%=u.username %></td>
                <td><%=u.password %></td>
                <td><%=u.nickname %></td>
                <td><%=u.gender?"":"" %></td>
                <td><%=u.birthday.ToString("yyyy-MM-dd") %></td>
                <td><%=u.Nation1.Name %></td>
                <td><%=u.area %></td>
                <td>
                    <a href="Home/Update/<%=u.username %>">修改</a>
                    <a href="Home/Delete/<%=u.username %>">删除</a>
                </td>
            </tr>
            <%
                } 
            %>
        </table>
    
        <a href="Home/Insert">添加</a>
    </body>
    </html>
    Views_Home_Index
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Insert</title>
    </head>
    <body>
        <form action="Insert1" method="post">
            <div>
               <h1>添加页面</h1> 
                用户名:<input type="text" name="username"/><br /><br />&nbsp;码:<input type="text" name="password"/><br /><br />&nbsp;称:<input type="text" name="nickname"/><br /><br />&nbsp;别:<input type="text" name="gender"/><br /><br />&nbsp;日:<input type="text" name="birthday"/><br /><br />&nbsp;族:<input type="text" name="nation"/><br /><br />&nbsp;区:<input type="text" name="area"/><br /><br />
                <input type="submit" value="添加"/>
            </div>
        </form>
    </body>
    </html>
    Views_Home_Insert
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ Import Namespace="Mvc1.Models" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Update</title>
    </head>
    <body>
        <form action="/Home/Update1" method="post">
            <%
                Users u = ViewBag.data as Users;
                if (u != null)
                {
            %>
    
            <div>
                <h1>修改页面</h1>
                用户名:<input type="text" name="username" value="<%=u.username %>"/><br />
                <br />&nbsp;码:<input type="text" name="password" value="<%=u.password %>"/><br />
                <br />&nbsp;称:<input type="text" name="nickname" value="<%=u.nickname %>"/><br />
                <br />&nbsp;别:<input type="text" name="gender" value="<%=u.gender %>"/><br />
                <br />&nbsp;日:<input type="text" name="birthday" value="<%=u.birthday %>"/><br />
                <br />&nbsp;族:<input type="text" name="nation" value="<%=u.nation %>"/><br />
                <br />&nbsp;区:<input type="text" name="area" value="<%=u.area %>"/><br />
                <br />
                <input type="submit" value="修改" />
            </div>
    
            <% 
                }
                else
                {
            %>
    
            <h1>未查询到数据!</h1>
            
            <%
                }
            %>
        </form>
    </body>
    </html>
    Views_Home_Update

  • 相关阅读:
    工单相关函数
    ABAP 没有保存的长文本,如何取值
    小细节
    DEMO程序 排序
    ABAP 中的消息类型和处理方式
    那些 诡异的表格
    F4搜索帮助~出口函数
    使用XML的方式导出EXCEL
    更改销售订单某些字段和按钮 不可编辑
    ABAP-如何读取内表的字段名称
  • 原文地址:https://www.cnblogs.com/xiao55/p/6050893.html
Copyright © 2011-2022 走看看