• MVC 中aspx的增删改查


    先看总体结构

    LInQ

    #pragma warning disable 1591
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     此代码由工具生成。
    //     运行时版本:4.0.30319.17929
    //
    //     对此文件的更改可能会导致不正确的行为,并且如果
    //     重新生成代码,这些更改将会丢失。
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace MvcApplication3.Models
    {
        using System.Data.Linq;
        using System.Data.Linq.Mapping;
        using System.Data;
        using System.Collections.Generic;
        using System.Reflection;
        using System.Linq;
        using System.Linq.Expressions;
        using System.ComponentModel;
        using System;
        
        
        [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="linq")]
        public partial class linqDataContext : System.Data.Linq.DataContext
        {
            
            private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
            
        #region 可扩展性方法定义
        partial void OnCreated();
        partial void InsertNation(Nation instance);
        partial void UpdateNation(Nation instance);
        partial void DeleteNation(Nation instance);
        partial void InsertUsers(Users instance);
        partial void UpdateUsers(Users instance);
        partial void DeleteUsers(Users instance);
        #endregion
            
            public linqDataContext() : 
                    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["linqConnectionString"].ConnectionString, mappingSource)
            {
                OnCreated();
            }
            
            public linqDataContext(string connection) : 
                    base(connection, mappingSource)
            {
                OnCreated();
            }
            
            public linqDataContext(System.Data.IDbConnection connection) : 
                    base(connection, mappingSource)
            {
                OnCreated();
            }
            
            public linqDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
                    base(connection, mappingSource)
            {
                OnCreated();
            }
            
            public linqDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
                    base(connection, mappingSource)
            {
                OnCreated();
            }
            
            public System.Data.Linq.Table<Nation> Nation
            {
                get
                {
                    return this.GetTable<Nation>();
                }
            }
            
            public System.Data.Linq.Table<Users> Users
            {
                get
                {
                    return this.GetTable<Users>();
                }
            }
        }
        
        [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Nation")]
        public partial class Nation : INotifyPropertyChanging, INotifyPropertyChanged
        {
            
            private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
            
            private string _NationCode;
            
            private string _NationName;
            
            private EntitySet<Users> _Users;
            
        #region 可扩展性方法定义
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnNationCodeChanging(string value);
        partial void OnNationCodeChanged();
        partial void OnNationNameChanging(string value);
        partial void OnNationNameChanged();
        #endregion
            
            public Nation()
            {
                this._Users = new EntitySet<Users>(new Action<Users>(this.attach_Users), new Action<Users>(this.detach_Users));
                OnCreated();
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NationCode", DbType="NVarChar(20) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]
            public string NationCode
            {
                get
                {
                    return this._NationCode;
                }
                set
                {
                    if ((this._NationCode != value))
                    {
                        this.OnNationCodeChanging(value);
                        this.SendPropertyChanging();
                        this._NationCode = value;
                        this.SendPropertyChanged("NationCode");
                        this.OnNationCodeChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NationName", DbType="NVarChar(20)")]
            public string NationName
            {
                get
                {
                    return this._NationName;
                }
                set
                {
                    if ((this._NationName != value))
                    {
                        this.OnNationNameChanging(value);
                        this.SendPropertyChanging();
                        this._NationName = value;
                        this.SendPropertyChanged("NationName");
                        this.OnNationNameChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Nation_Users", Storage="_Users", ThisKey="NationCode", OtherKey="Nation")]
            public EntitySet<Users> Users
            {
                get
                {
                    return this._Users;
                }
                set
                {
                    this._Users.Assign(value);
                }
            }
            
            public event PropertyChangingEventHandler PropertyChanging;
            
            public event PropertyChangedEventHandler PropertyChanged;
            
            protected virtual void SendPropertyChanging()
            {
                if ((this.PropertyChanging != null))
                {
                    this.PropertyChanging(this, emptyChangingEventArgs);
                }
            }
            
            protected virtual void SendPropertyChanged(String propertyName)
            {
                if ((this.PropertyChanged != null))
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            
            private void attach_Users(Users entity)
            {
                this.SendPropertyChanging();
                entity.Nation1 = this;
            }
            
            private void detach_Users(Users entity)
            {
                this.SendPropertyChanging();
                entity.Nation1 = null;
            }
        }
        
        [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Users")]
        public partial class Users : INotifyPropertyChanging, INotifyPropertyChanged
        {
            
            private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
            
            private string _UserName;
            
            private string _Password;
            
            private System.Nullable<bool> _Sex;
            
            private string _NickName;
            
            private System.Nullable<System.DateTime> _Birthday;
            
            private string _Nation;
            
            private EntityRef<Nation> _Nation1;
            
        #region 可扩展性方法定义
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnUserNameChanging(string value);
        partial void OnUserNameChanged();
        partial void OnPasswordChanging(string value);
        partial void OnPasswordChanged();
        partial void OnSexChanging(System.Nullable<bool> value);
        partial void OnSexChanged();
        partial void OnNickNameChanging(string value);
        partial void OnNickNameChanged();
        partial void OnBirthdayChanging(System.Nullable<System.DateTime> value);
        partial void OnBirthdayChanged();
        partial void OnNationChanging(string value);
        partial void OnNationChanged();
        #endregion
            
            public Users()
            {
                this._Nation1 = default(EntityRef<Nation>);
                OnCreated();
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_UserName", DbType="NVarChar(20) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]
            public string UserName
            {
                get
                {
                    return this._UserName;
                }
                set
                {
                    if ((this._UserName != value))
                    {
                        this.OnUserNameChanging(value);
                        this.SendPropertyChanging();
                        this._UserName = value;
                        this.SendPropertyChanged("UserName");
                        this.OnUserNameChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Password", DbType="NVarChar(20)")]
            public string Password
            {
                get
                {
                    return this._Password;
                }
                set
                {
                    if ((this._Password != value))
                    {
                        this.OnPasswordChanging(value);
                        this.SendPropertyChanging();
                        this._Password = value;
                        this.SendPropertyChanged("Password");
                        this.OnPasswordChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Sex", DbType="Bit")]
            public System.Nullable<bool> Sex
            {
                get
                {
                    return this._Sex;
                }
                set
                {
                    if ((this._Sex != value))
                    {
                        this.OnSexChanging(value);
                        this.SendPropertyChanging();
                        this._Sex = value;
                        this.SendPropertyChanged("Sex");
                        this.OnSexChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NickName", DbType="NVarChar(20)")]
            public string NickName
            {
                get
                {
                    return this._NickName;
                }
                set
                {
                    if ((this._NickName != value))
                    {
                        this.OnNickNameChanging(value);
                        this.SendPropertyChanging();
                        this._NickName = value;
                        this.SendPropertyChanged("NickName");
                        this.OnNickNameChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Birthday", DbType="DateTime")]
            public System.Nullable<System.DateTime> Birthday
            {
                get
                {
                    return this._Birthday;
                }
                set
                {
                    if ((this._Birthday != value))
                    {
                        this.OnBirthdayChanging(value);
                        this.SendPropertyChanging();
                        this._Birthday = value;
                        this.SendPropertyChanged("Birthday");
                        this.OnBirthdayChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Nation", DbType="NVarChar(20)")]
            public string Nation
            {
                get
                {
                    return this._Nation;
                }
                set
                {
                    if ((this._Nation != value))
                    {
                        if (this._Nation1.HasLoadedOrAssignedValue)
                        {
                            throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                        }
                        this.OnNationChanging(value);
                        this.SendPropertyChanging();
                        this._Nation = value;
                        this.SendPropertyChanged("Nation");
                        this.OnNationChanged();
                    }
                }
            }
            
            [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Nation_Users", Storage="_Nation1", ThisKey="Nation", OtherKey="NationCode", IsForeignKey=true)]
            public Nation Nation1
            {
                get
                {
                    return this._Nation1.Entity;
                }
                set
                {
                    Nation previousValue = this._Nation1.Entity;
                    if (((previousValue != value) 
                                || (this._Nation1.HasLoadedOrAssignedValue == false)))
                    {
                        this.SendPropertyChanging();
                        if ((previousValue != null))
                        {
                            this._Nation1.Entity = null;
                            previousValue.Users.Remove(this);
                        }
                        this._Nation1.Entity = value;
                        if ((value != null))
                        {
                            value.Users.Add(this);
                            this._Nation = value.NationCode;
                        }
                        else
                        {
                            this._Nation = default(string);
                        }
                        this.SendPropertyChanged("Nation1");
                    }
                }
            }
            
            public event PropertyChangingEventHandler PropertyChanging;
            
            public event PropertyChangedEventHandler PropertyChanged;
            
            protected virtual void SendPropertyChanging()
            {
                if ((this.PropertyChanging != null))
                {
                    this.PropertyChanging(this, emptyChangingEventArgs);
                }
            }
            
            protected virtual void SendPropertyChanged(String propertyName)
            {
                if ((this.PropertyChanged != null))
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
    #pragma warning restore 1591
    View Code

    属性扩展(Users)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcApplication3.Models
    {
        public partial class Users
        {
            public string color//属性扩展
            {
                get
                {
                    string s = "";
                    if (this._Sex.Value == true)
                    {
                        s = "background-color:gray";
                    }
                    else
                    {
    
                        s = "background-color:red";
                    }
    
    
    
                    return s;
                
                }
            }
    
    
    
        }
    }
    View Code

    控制层(Home)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcApplication3.Models;
    
    namespace MvcApplication3.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
    
            public ActionResult Index()//展示主页面
            {
                return View();
            }
    
            public ActionResult insert()//展示添加界面
            {
                return View();
            }
            //执行添加功能
            public ActionResult insert1(string username,string password,string nickname,string sex,string birthday,string nation)//此处的字段变量必须与form表单的name值一样
            {
                Users u = new Users();
                u.UserName = username;
                u.Password = password;
                u.NickName = nickname;
                u.Sex=Convert.ToBoolean(sex);
                u.Birthday = Convert.ToDateTime(birthday);
                u.Nation = nation;
                new UserData().insert(u);
                return RedirectToAction("Index","Home");
            }
            //执行删除功能
            public ActionResult delete( string id)//“路由”传值方式,必须是id
            {
                
                new UserData().delete(id);
                return RedirectToAction("Index");//由于删除界面和主页面都是同一个,所以跳转界面的参数写一个就可以了
                
            }
    
            public ActionResult update(string id)
            {
                Users u = new UserData().dan(id);
    
                ViewBag.hehe = u;//视图背包,把里面的内容“运”到该视图中去,作用仅限于所在视图
    
                return View();
            }
    
            public ActionResult update1(Users u)
            {
                bool a= new UserData().update(u);
                return RedirectToAction("Index");
            }
    
    
    
    
        }
    }
    View Code

    模型层

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcApplication3.Models
    {
        public partial class UserData
        {
    
            linqDataContext cnn = new linqDataContext();
            public List<Users> all()//查询全部方法
            {
    
                return cnn.Users.ToList();
            }
            public void insert(Users u)//插入
            {
                cnn.Users.InsertOnSubmit(u);
                cnn.SubmitChanges();
            }
            public Users dan(string uname)//查询单条信息
            {
                return cnn.Users.Where(r => r.UserName == uname).FirstOrDefault();
            }
            public void delete(string id)//删除方法
            {
                Users u = cnn.Users.Where(r => r.UserName == id).FirstOrDefault();
                if (u != null)
                {
                    cnn.Users.DeleteOnSubmit(u);
                    cnn.SubmitChanges();
                }
            }
    
            public bool update(Users uuu)//修改信息
            {
                Users u = uuu;
                bool a = false;
                Users uu = cnn.Users.Where(r => r.UserName == u.UserName).FirstOrDefault();
                if (uu != null)
                {
                    uu.UserName = u.UserName;
                    uu.Password = u.Password;
                    uu.NickName = u.NickName;
                    uu.Sex = u.Sex;
                    uu.Birthday = u.Birthday;
                    uu.Nation = u.Nation;
                    
                    try
                    {
                        cnn.SubmitChanges();
                        a = true;
                    }
                    catch
                    { }
                }
                return a;
            
            }
    
        }
    }
    View Code

    视图层(Index)

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ Import Namespace="MvcApplication3.Models" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            <table  style="100%;background-color:aqua;text-align:center;color:white;">
                <tr style="color:white;background-color:orange;">
                    <td>姓名</td>
                    <td>密码</td>
                    <td>昵称</td>
                    <td>性别</td>
                    <td>生日</td>
                    <td>民族</td>
                    <td>操作</td>
                </tr>
                <%
                    List<Users> ulist=new UserData().all();
                    foreach (Users u in ulist)
                    { 
                     %>
                 <tr style="color:white;<%=u.color %>">
                    <td><%=u.UserName %></td>
                    <td><%=u.Password %></td>
                     <td><%=u.NickName %></td>
                     <td><%=u.Sex.Value?"":"" %></td>
                     <td><%=u.Birthday.Value.ToString("yyyy年MM月dd日") %></td>
                    <td><%=u.Nation1.NationName %></td>
                       <td><a href="Home/update/<%=u.UserName %>">修改</a></td>
                     <td><a href="Home/delete/<%=u.UserName %>">删除</a></td><%--注意“路由”传值Home/delete/值--%>
                </tr>
                <%
                }
                     %>
    
            </table>
            <a href="/Home/insert">添加</a>
    
        </div>
    </body>
    </html>
    View Code

    视图层(insert)

    <%@ 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><%--添加界面--%>
        <div>
            <h1>添加用户</h1>
            <form name="form1" action="insert1" method="post">
            用户名<input type="text" name="username" /><br />
            密码<input type="text" name="password" /><br />
            昵称<input type="text" name="nickname" /><br />
            性别<input type="text" name="sex" /><br />
            生日<input type="text" name="birthday" /><br />
            民族<input type="text" name="nation" /><br />
            <input type="submit" value="提交" />
                </form>
        </div>
    </body>
    </html>
    View Code

    视图层(update)

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ import Namespace="MvcApplication3.Models" %>>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>update</title>
    </head>
    <body>
        <div>
              <h1>修改用户</h1>
            <form name="form1" action="/Home/update1" method="post">
              <%
                  Users u = ViewBag.hehe;//视图包取值
                   %>>
            用户名<input type="text" name="username"  value="<%=u.UserName %>" /><br />
            密码<input type="password" name="password"  value="<%=u.Password %>" /><br />
            昵称<input type="text" name="nickname" value="<%=u.NickName %>" /><br />
            性别<input type="text" name="sex" value="<%=u.Sex %>" /><br />
            生日<input type="text" name="birthday" value="<%=u.Birthday %>" /><br />
            民族<input type="text" name="nation" value="<%=u.Nation %>" /><br />
            <input type="submit" value="确认修改" />
                </form>
        </div>
    </body>
    </html>
    View Code

    完!!

  • 相关阅读:
    index()方法
    extend()方法
    count()方法
    copy()方法
    clear()方法
    append()方法
    IE botton 点击文字下沉
    IE滚动条
    关闭windows10自动更新
    vue文件名规范
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/6132123.html
Copyright © 2020-2023  润新知