先看总体结构

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

属性扩展(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; }
} }
}

控制层(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");
} }
}

模型层

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; } }
}

视图层(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="width: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>

视图层(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>

视图层(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>

完!!

MVC 中aspx的增删改查的更多相关文章

  1. MVC中使用EF增删改查,简单的例子

    //这个是分页数据和总页数类 public class SummaryBase<TModel> { public SummaryBase(); public IList<TModel ...

  2. MVC与EasyUI结合增删改查

    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查   在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的 ...

  3. MVC无限级分类02,增删改查

    继上一篇"MVC无限级分类01,分层架构,引入缓存,完成领域模型与视图模型的映射",本篇开始MVC无限级分类的增删改查部分,源码在github. 显示和查询 使用datagrid显 ...

  4. 一、数据库表中字段的增删改查,二、路由基础.三、有名无名分组.四、多app共存的路由分配.五、多app共存时模板冲突问题.六、创建app流程.七、路由分发.八、路由别名,九、名称空间.十、反向解析.十一、2.x新特性.十二、自定义转换器

    一.数据库表中字段的增删改查 ''' 直接在modules中对字段进行增删改查 然后在tools下点击Run manage.py Task执行makemigrations和migrate 注意在执行字 ...

  5. MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

    设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...

  6. MVC模式:实现数据库中数据的增删改查功能

    *.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查

    系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...

  8. 关于MVC工厂模式的增删改查sql存储过程

    这里MVC中用到了反射,工厂,泛型,接口 在搭建框架的时候,除了MVC的三层以外,还有泛型的接口层和工厂层 下面是dal层调用sql存储过程,增删改查,dal层继承了接口层,实现了接口层里面的方法 1 ...

  9. 数据库中简单的增删改查(CRUD)

    一切都是基于数据,而对数据的管理都离不开数据库.最近学到数据库的简单操作,所以写下这篇文章,总结一下学习到的知识.浅陋之处,多多见谅. 补充一下:一直弄不清SQL Server,Mysql ,以及Or ...

随机推荐

  1. 2016HUAS暑假集训训练2 J - 今年暑假不AC

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/J 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...

  2. android之消息机制(二)

    消息通道:Looper 首先编写main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk ...

  3. js 所有事件列表

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  4. scrum站立会议学习

    项目:连连看游戏 小组名称:临时小组 组长:张政 小组成员: 李权 武志远 张政 张金生 MASTER:张政 会议内容: 一.已完成项: 1.根据先前的安排和计划完成了项目主要环境的搭建,配置好了基本 ...

  5. [译]line clampin让文字在指定的行数内省略号显示

    说明 (1)原文:http://css-tricks.com/line-clampin/ (2)非直译 需求: 当文字长度超过N行时,文字后面自动用省略号补齐. 比如,你有如下的HTML代码: < ...

  6. cordova-sqlite-plugin常用数据库操作

    ionic项目需要做本地缓存,研究了一番,总结出了常用的数据库操作: 1.新建数据库,用angularJS+ionic: //声明一个数据库对象 var db = null; //事件devicere ...

  7. 领导者/追随者(Leader/Followers)模型和半同步/半异步(half-sync/half-async)模型都是常用的客户-服务器编程模型

    领导者-追随者(Leader/Followers)模型的比喻 半同步/半异步模型和领导者/追随者模型的区别: 半同步/半异步模型拥有一个显式的待处理事件队列,而领导者-追随者模型没有一个显式的队列(很 ...

  8. Range

    欢迎转载,转载请注明出处,徽沪一郎. 概要 Scala中Range可以看成是List的特例,Range的包含的元素类型是Int, 本文介绍如何创建Range Range创建 方法一: val r1 = ...

  9. DS Tree 已知先序、中序 => 建树 => 求后序

    参考:二叉树--前序和中序得到后序 思路历程: 在最初敲的时候,经常会弄混preorder和midorder的元素位置.大体的思路就是在preorder中找到根节点(根节点在序列的左边),然后在mid ...

  10. hibernate学习(4)——实体配置详解

    1.实体 编写规则 提供一个无参数 public访问控制符的构造器 提供一个标识属性,映射数据表主键字段,hibernate以id识别,必须有主键 所有属性提供public访问控制符的 set  ge ...