.NET使用EF和MVC三层架构实现通讯录(包含源码,SQL Server数据库)
通讯录源码链接
提示:数据库可以根据图片列名自己创建
文章目录
.NET使用EF和MVC三层架构实现通讯录(包含源码,SQL Server数据库)前言一、数据库创建二、创建Model1.在Model新建项
三、Controllers的创建四、Views的创建1.Login2.User2.1Index.cshtml2.2Add.cshtml2.3Edit.cshtml
3.Group2.1 Index.cshtml2.2Add.cshtml2.3Edit.cshtml
4.Contact2.1 Index.cshtml2.2Add.cshtml2.3Edit.cshtml
5.Shared5.1_Layout.cshtml5.2Error.cshtml
总结
前言
提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、数据库创建
根据图片内容列名可自己创建 User Group Contact
二、创建Model
1.在Model新建项
操作如下(示例):
三、Controllers的创建
代码如下(示例):
LoginController
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Web
;
using System
.Web
.Mvc
;
using 通讯录
.Models
;
namespace 通讯录
.Controllers
{
public class LoginController
: Controller
{
Models
.DBAddressBookEntities db
= new Models
.DBAddressBookEntities();
public ActionResult
Login()
{
return View();
}
[HttpPost
]
public ActionResult
Login(string username
, string password
)
{
int n
= db
.Users
.Where(u
=> u
.UserName
== username
&& u
.PassWord
== password
).Count();
if (n
== 1)
{
var user
= db
.Users
.Where(u
=> u
.UserName
== username
).SingleOrDefault();
Session
["userid"] = user
.Id
;
return Redirect("/contact/index");
}
Response
.Write("<script>alert('用户名或密码错误')</script>");
return View();
}
}
}
UserController
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Web
;
using System
.Web
.Mvc
;
using 通讯录
.Models
;
namespace 通讯录
.Controllers
{
public class UserController : Controller
{
Models
.DBAddressBookEntities db
= new Models
.DBAddressBookEntities();
public ActionResult
Index()
{
List
<User
> userlist
= db
.Users
.OrderBy(u
=> u
.Id
).ToList();
return View(userlist
);
}
[HttpGet
]
public ActionResult
Add()
{
List
<SelectListItem
> lstrole
= new List
<SelectListItem
>();
lstrole
.Add(new SelectListItem
{ Text
= "--请选择角色--", Value
= "0" });
lstrole
.Add(new SelectListItem
{ Text
= "普通用户", Value
= "普通用户" });
lstrole
.Add(new SelectListItem
{ Text
= "管理员", Value
= "管理员" });
ViewBag
.lstrole
= lstrole
;
return View();
}
[HttpPost
]
public ActionResult
Add(string username
, string password
, string role
)
{
User model
= new User();
model
.UserName
= username
;
model
.PassWord
= password
;
model
.Role
= role
;
db
.Users
.Add(model
);
db
.SaveChanges();
return Redirect("/user/index");
}
public ActionResult
Delete(int Id
)
{
db
.Users
.Remove(db
.Users
.Find(Id
));
int n
= db
.SaveChanges();
return Redirect("/user/index");
}
[HttpGet
]
public ActionResult
Edit(int Id
)
{
User olduser
= db
.Users
.Find(Id
);
List
<SelectListItem
> lstrole
= new List
<SelectListItem
>();
lstrole
.Add(new SelectListItem
{ Text
= "--请选择角色--", Value
= "0" });
lstrole
.Add(new SelectListItem
{ Text
= "普通用户", Value
= "普通用户" });
lstrole
.Add(new SelectListItem
{ Text
= "管理员", Value
= "管理员" });
ViewBag
.lstrole
= lstrole
;
return View(olduser
);
}
public ActionResult
Edit(User user
)
{
if (ModelState
.IsValid
)
{
User olduser
= db
.Users
.Find(user
.Id
);
olduser
.UserName
= user
.UserName
;
olduser
.PassWord
= user
.PassWord
;
olduser
.Role
= user
.Role
;
int n
= db
.SaveChanges();
if (n
> 0)
{
return Redirect("/user/index");
}
}
return View(user
);
}
}
}
GroupController
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Web
;
using System
.Web
.Mvc
;
using 通讯录
.Models
;
namespace 通讯录
.Controllers
{
public class GroupController : Controller
{
Models
.DBAddressBookEntities db
= new Models
.DBAddressBookEntities();
public ActionResult
Index()
{
List
<Group
> grouplist
= db
.Groups
.OrderBy(u
=> u
.Id
).ToList();
return View(grouplist
);
}
[HttpGet
]
public ActionResult
Add()
{
return View();
}
[HttpPost
]
public ActionResult
Add(string groupid
, string groupname
, string remarks
)
{
Group model
= new Group();
model
.GroupId
= groupid
;
model
.GroupName
= groupname
;
model
.Remarks
= remarks
;
db
.Groups
.Add(model
);
db
.SaveChanges();
return Redirect("/group/index");
}
public ActionResult
Delete(int Id
)
{
db
.Groups
.Remove(db
.Groups
.Find(Id
));
int n
= db
.SaveChanges();
return Redirect("/group/index");
}
[HttpGet
]
public ActionResult
Edit(int Id
)
{
Group oldgroup
= db
.Groups
.Find(Id
);
return View(oldgroup
);
}
public ActionResult
Edit(Group group
)
{
if (ModelState
.IsValid
)
{
Group oldgroup
= db
.Groups
.Find(group
.Id
);
oldgroup
.GroupId
= group
.GroupId
;
oldgroup
.GroupName
= group
.GroupName
;
oldgroup
.Remarks
= group
.Remarks
;
int n
= db
.SaveChanges();
if (n
> 0)
{
return Redirect("/group/index");
}
}
return View(group
);
}
}
}
ContactController
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Web
;
using System
.Web
.Mvc
;
using System
.Web
.WebPages
;
using 通讯录
.Models
;
namespace 通讯录
.Controllers
{
public class ContactController : Controller
{
Models
.DBAddressBookEntities db
= new Models
.DBAddressBookEntities();
public ActionResult
Index()
{
List
<Contact
> contactlist
= db
.Contacts
.OrderBy(u
=> u
.Id
).ToList();
return View(contactlist
);
}
[HttpGet
]
public ActionResult
Add()
{
List
<SelectListItem
> lsgender
= new List
<SelectListItem
>();
lsgender
.Add(new SelectListItem
{ Text
= "--请选择性别--", Value
= "0" });
lsgender
.Add(new SelectListItem
{ Text
= "男", Value
= "男" });
lsgender
.Add(new SelectListItem
{ Text
= "女", Value
= "女" });
ViewBag
.lsgender
= lsgender
;
List
<SelectListItem
> lstgroupId
= new List
<SelectListItem
>();
List
<Group
> groupId
= db
.Groups
.OrderBy(t
=> t
.Id
).ToList();
lstgroupId
.Add(new SelectListItem() { Text
= "--请选择分组编号--", Value
= "0" });
for (int i
= 0; i
< groupId
.Count
; i
++)
{
lstgroupId
.Add(new SelectListItem() { Text
= groupId
[i
].GroupId
, Value
= groupId
[i
].GroupId
.ToString() });
}
ViewBag
.lstgroupId
= lstgroupId
;
return View();
}
[HttpPost
]
public ActionResult
Add(Contact contact
)
{
List
<SelectListItem
> lsgender
= new List
<SelectListItem
>();
lsgender
.Add(new SelectListItem
{ Text
= "--请选择性别--", Value
= "0" });
lsgender
.Add(new SelectListItem
{ Text
= "男", Value
= "男" });
lsgender
.Add(new SelectListItem
{ Text
= "女", Value
= "女" });
ViewBag
.lsgender
= lsgender
;
List
<SelectListItem
> lstgroupId
= new List
<SelectListItem
>();
List
<Group
> groupId
= db
.Groups
.OrderBy(t
=> t
.Id
).ToList();
lstgroupId
.Add(new SelectListItem() { Text
= "--请选择分组编号--", Value
= "0" });
for (int i
= 0; i
< groupId
.Count
; i
++)
{
lstgroupId
.Add(new SelectListItem() { Text
= groupId
[i
].GroupId
, Value
= groupId
[i
].GroupId
.ToString() });
}
ViewBag
.lstgroupId
= lstgroupId
;
Contact model
= new Contact();
db
.Contacts
.Add(contact
);
db
.SaveChanges();
return Redirect("/contact/index");
}
public ActionResult
Delete(int Id
)
{
db
.Contacts
.Remove(db
.Contacts
.Find(Id
));
int n
= db
.SaveChanges();
return Redirect("/contact/index");
}
[HttpGet
]
public ActionResult
Edit(int Id
)
{
Contact oldContact
= db
.Contacts
.Find(Id
);
List
<SelectListItem
> lsgender
= new List
<SelectListItem
>();
lsgender
.Add(new SelectListItem
{ Text
= "--请选择性别--", Value
= "0" });
lsgender
.Add(new SelectListItem
{ Text
= "男", Value
= "男" });
lsgender
.Add(new SelectListItem
{ Text
= "女", Value
= "女" });
ViewBag
.lsgender
= lsgender
;
List
<SelectListItem
> lstgroupId
= new List
<SelectListItem
>();
List
<Group
> groupId
= db
.Groups
.OrderBy(t
=> t
.Id
).ToList();
lstgroupId
.Add(new SelectListItem() { Text
= "--请选择分组编号--", Value
= "0" });
for (int i
= 0; i
< groupId
.Count
; i
++)
{
lstgroupId
.Add(new SelectListItem() { Text
= groupId
[i
].GroupId
, Value
= groupId
[i
].GroupId
.ToString() });
}
ViewBag
.lstgroupId
= lstgroupId
;
return View(oldContact
);
}
public ActionResult
Edit(Contact contact
)
{
List
<SelectListItem
> lsgender
= new List
<SelectListItem
>();
lsgender
.Add(new SelectListItem
{ Text
= "--请选择性别--", Value
= "0" });
lsgender
.Add(new SelectListItem
{ Text
= "男", Value
= "男" });
lsgender
.Add(new SelectListItem
{ Text
= "女", Value
= "女" });
ViewBag
.lsgender
= lsgender
;
List
<SelectListItem
> lstgroupId
= new List
<SelectListItem
>();
List
<Group
> groupId
= db
.Groups
.OrderBy(t
=> t
.Id
).ToList();
lstgroupId
.Add(new SelectListItem() { Text
= "--请选择分组编号--", Value
= "0" });
for (int i
= 0; i
< groupId
.Count
; i
++)
{
lstgroupId
.Add(new SelectListItem() { Text
= groupId
[i
].GroupId
, Value
= groupId
[i
].GroupId
.ToString() });
}
ViewBag
.lstgroupId
= lstgroupId
;
Contact oldcontact
= db
.Contacts
.Find(contact
.Id
);
oldcontact
.Name
= contact
.Name
;
oldcontact
.Gender
= contact
.Gender
;
oldcontact
.Birthday
= contact
.Birthday
;
oldcontact
.MobileNumber
= contact
.MobileNumber
;
oldcontact
.HomeNumber
= contact
.HomeNumber
;
oldcontact
.HomeAddress
= contact
.HomeAddress
;
oldcontact
.QQ
= contact
.QQ
;
oldcontact
.Email
= contact
.Email
;
oldcontact
.MSN
= contact
.MSN
;
oldcontact
.CompanyNumber
= contact
.CompanyNumber
;
oldcontact
.CompanyAddress
= oldcontact
.CompanyAddress
;
oldcontact
.GroupId
= contact
.GroupId
;
oldcontact
.Remarks
= contact
.Remarks
;
int n
= db
.SaveChanges();
if (n
> 0)
{
return Redirect("/contact/index");
}
return View(contact
);
}
}
}
四、Views的创建
代码如下(示例):
1.Login
Login.cshtml
@
{
Layout
= null
;
}
<!DOCTYPE html
>
<html
>
<head
>
<meta name
="viewport" content
="width=device-width" />
<title
>登录
</title
>
@Styles
.Render("~/Content/css")
@Scripts
.Render("~/bundles/modernizr")
</head
>
<body
>
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">通讯录登陆
</h3
>
</div
>
<div
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">用户名
</label
>
<div
class="col-sm-10">
<input type
="text" class="form-control" id
="username" name
="username" placeholder
="请输入用户名">
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">密码
</label
>
<div
class="col-sm-10">
<input type
="text" class="form-control" id
="password" name
="password" placeholder
="请输入密码">
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">登录
</button
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
@Scripts
.Render("~/bundles/jquery")
@Scripts
.Render("~/bundles/bootstrap")
</body
>
</html
>
2.User
2.1Index.cshtml
@model List
<User
>
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Index";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<table
class="table table-hover">
<caption
>用户列表
</caption
>
<thead
>
<tr
>
<th
>Id
</th
>
<th
>用户名
</th
>
<th
>密码
</th
>
<th
>角色
</th
>
<th
>操作
</th
>
</tr
>
</thead
>
<tbody
>
@
foreach(User user in Model
)
{
<tr
>
<td
>@user
.Id
</td
>
<td
>@user
.UserName
</td
>
<td
>@user
.PassWord
</td
>
<td
>@user
.Role
</td
>
<td
>
<a href
="/user/edit/@user.Id" style
="margin-right:5px;">修改
</a
>
<a href
="/user/delete/@user.Id" onclick
="return confirm('确定要删除用户 @user.UserName 吗?');">删除
</a
>
</td
>
</tr
>
}
</tbody
>
</table
>
2.2Add.cshtml
@model User
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Add";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">用户信息添加
</h3
>
</div
>
<iv
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">用户名
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.UserName
, new { @
class = "form-control", placeholder
= "请输入用户名" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">密码
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.PassWord
, new { @
class = "form-control", placeholder
= "请输入密码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">角色
</label
>
<div
class="col-sm-10">
@Html
.DropDownListFor(u
=> u
.Role
, ViewBag
.lstrole as IEnumerable
<SelectListItem
>, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">添加
</button
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
2.3Edit.cshtml
@model User
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Edit";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">用户信息修改
</h3
>
</div
>
<iv
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">用户名
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.UserName
, new { @
class = "form-control", placeholder
= "请输入用户名" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">密码
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.PassWord
, new { @
class = "form-control", placeholder
= "请输入密码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">角色
</label
>
<div
class="col-sm-10">
@Html
.DropDownListFor(u
=> u
.Role
, ViewBag
.lstrole as IEnumerable
<SelectListItem
>, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">修改
</button
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
3.Group
2.1 Index.cshtml
@model List
<Group
>
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Index";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<table
class="table table-hover">
<caption
>分组列表
</caption
>
<thead
>
<tr
>
<th
>Id
</th
>
<th
>分组编号
</th
>
<th
>分钟名称
</th
>
<th
>备注
</th
>
<th
>操作
</th
>
</tr
>
</thead
>
<tbody
>
@foreach
(Group group in Model
)
{
<tr
>
<td
>@group
.Id
</td
>
<td
>@group
.GroupId
</td
>
<td
>@group
.GroupName
</td
>
<td
>@group
.Remarks
</td
>
<td
>
<a href
="/group/edit/@group.Id" style
="margin-right:5px;">修改
</a
>
<a href
="/group/delete/@group.Id" onclick
="return confirm('确定要删除分组 @group.GroupName 吗?');">删除
</a
>
</td
>
</tr
>
}
</tbody
>
</table
>
2.2Add.cshtml
@model Group
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Add";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">分组信息添加
</h3
>
</div
>
<iv
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">分组编号
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.GroupId
, new { @
class = "form-control", placeholder
= "请输入分组编号" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">分组名称
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.GroupName
, new { @
class = "form-control", placeholder
= "请输入分组名称" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">备注
</label
>
<div
class="col-sm-10">
@Html
.TextAreaFor(u
=> u
.Remarks
, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">添加
</button
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
2.3Edit.cshtml
@model Group
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Edit";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">分组信息修改
</h3
>
</div
>
<iv
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">分组编号
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.GroupId
, new { @
class = "form-control", placeholder
= "请输入分组编号" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">分组名称
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.GroupName
, new { @
class = "form-control", placeholder
= "请输入分组名称" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">备注
</label
>
<div
class="col-sm-10">
@Html
.TextAreaFor(u
=> u
.Remarks
, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">修改
</button
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
4.Contact
2.1 Index.cshtml
@model List
<Contact
>
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Index";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<table
class="table table-hover">
<caption
>分组列表
</caption
>
<thead
>
<tr
>
<th
>Id
</th
>
<th
>姓名
</th
>
<th
>性别
</th
>
<th
>生日
</th
>
<th
>手机号码
</th
>
<th
>座机号码
</th
>
<th
>家庭地址
</th
>
<th
>QQ
</th
>
<th
>Email
</th
>
<th
>MSN
</th
>
<th
>公司电话
</th
>
<th
>公司地址
</th
>
<th
>分组编号
</th
>
<th
>备注
</th
>
<th
>操作
</th
>
</tr
>
</thead
>
<tbody
>
@foreach
(Contact contact in Model
)
{
<tr
>
<td
>@contact
.Id
</td
>
<td
>@contact
.Name
</td
>
<td
>@contact
.Gender
</td
>
<td
>@contact
.Birthday
</td
>
<td
>@contact
.MobileNumber
</td
>
<td
>@contact
.HomeNumber
</td
>
<td
>@contact
.HomeAddress
</td
>
<td
>@contact
.QQ
</td
>
<td
>@contact
.Email
</td
>
<td
>@contact
.MSN
</td
>
<td
>@contact
.CompanyNumber
</td
>
<td
>@contact
.CompanyAddress
</td
>
<td
>@contact
.GroupId
</td
>
<td
>@contact
.Remarks
</td
>
<td
>
<a href
="/contact/edit/@contact.Id" style
="margin-right:5px;">修改
</a
>
<a href
="/contact/delete/@contact.Id" onclick
="return confirm('确定要删除联系人 @contact.Name 吗?');">删除
</a
>
</td
>
</tr
>
}
</tbody
>
</table
>
2.2Add.cshtml
@model Contact
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Add";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">联系人信息添加
</h3
>
</div
>
<iv
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">姓名
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.Name
, new { @
class = "form-control", placeholder
= "请输入联系人姓名" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">性别
</label
>
<div
class="col-sm-10">
@Html
.DropDownListFor(u
=> u
.Gender
, ViewBag
.lsgender as IEnumerable
<SelectListItem
>, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">生日
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.Birthday
, new { @
class = "form-control", placeholder
= "例如:2000-01-01" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">手机号码
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.MobileNumber
, new { @
class = "form-control", placeholder
= "请输入手机号码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">座机号码
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.HomeNumber
, new { @
class = "form-control", placeholder
= "请输入座机号码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">家庭地址
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.HomeAddress
, new { @
class = "form-control", placeholder
= "请输入家庭地址" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">QQ
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.QQ
, new { @
class = "form-control", placeholder
= "请输入QQ号码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">Email
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.Email
, new { @
class = "form-control", placeholder
= "请输入电子邮箱" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">MSN
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.MSN
, new { @
class = "form-control", placeholder
= "请输入MSN" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">公司电话
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.CompanyNumber
, new { @
class = "form-control", placeholder
= "请输入公司电话" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">公司地址
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.CompanyAddress
, new { @
class = "form-control", placeholder
= "请输入公司地址" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">分组编号
</label
>
<div
class="col-sm-10">
@Html
.DropDownListFor(u
=> u
.GroupId
, ViewBag
.lstgroupId as IEnumerable
<SelectListItem
>, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">备注
</label
>
<div
class="col-sm-10">
@Html
.TextAreaFor(u
=> u
.Remarks
, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">添加
</button
>
</div
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
2.3Edit.cshtml
@model Contact
@
using 通讯录
.Models
@
{
ViewBag
.Title
= "Edit";
Layout
= "~/Views/Shared/_Layout.cshtml";
}
<div
class="container">
<div
class="panel panel-success">
<div
class="panel-heading">
<h3
class="panel-title">联系人信息修改
</h3
>
</div
>
<iv
class="panel-body">
<form
class="form-horizontal" role
="form" method
="post">
<div
class="form-group">
<label
for="firstname" class="col-sm-2 control-label">姓名
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.Name
, new { @
class = "form-control", placeholder
= "请输入联系人姓名" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">性别
</label
>
<div
class="col-sm-10">
@Html
.DropDownListFor(u
=> u
.Gender
, ViewBag
.lsgender as IEnumerable
<SelectListItem
>, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">生日
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.Birthday
, new { @
class = "form-control", placeholder
= "例如:2000-01-01" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">手机号码
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.MobileNumber
, new { @
class = "form-control", placeholder
= "请输入手机号码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">座机号码
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.HomeNumber
, new { @
class = "form-control", placeholder
= "请输入座机号码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">家庭地址
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.HomeAddress
, new { @
class = "form-control", placeholder
= "请输入家庭地址" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">QQ
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.QQ
, new { @
class = "form-control", placeholder
= "请输入QQ号码" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">Email
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.Email
, new { @
class = "form-control", placeholder
= "请输入电子邮箱" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">MSN
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.MSN
, new { @
class = "form-control", placeholder
= "请输入MSN" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">公司电话
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.CompanyNumber
, new { @
class = "form-control", placeholder
= "请输入公司电话" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">公司地址
</label
>
<div
class="col-sm-10">
@Html
.TextBoxFor(u
=> u
.CompanyAddress
, new { @
class = "form-control", placeholder
= "请输入公司地址" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">分组编号
</label
>
<div
class="col-sm-10">
@Html
.DropDownListFor(u
=> u
.GroupId
, ViewBag
.lstgroupId as IEnumerable
<SelectListItem
>, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<label
for="lastname" class="col-sm-2 control-label">备注
</label
>
<div
class="col-sm-10">
@Html
.TextAreaFor(u
=> u
.Remarks
, new { @
class = "form-control" })
</div
>
</div
>
<div
class="form-group">
<div
class="col-sm-offset-2 col-sm-10">
<button type
="submit" class="btn btn-default">修改
</button
>
</div
>
</div
>
</div
>
</form
>
</div
>
</div
>
</div
>
5.Shared
5.1_Layout.cshtml
<!DOCTYPE html
>
<html
>
<head
>
<meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8"/>
<meta charset
="utf-8" />
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>@ViewBag
.Title
- 我的 ASP
.NET 应用程序
</title
>
@Styles
.Render("~/Content/css")
@Scripts
.Render("~/bundles/modernizr")
</head
>
<body
>
<div
class="navbar navbar-inverse navbar-fixed-top">
<div
class="container">
<div
class="navbar-header">
<button type
="button" class="navbar-toggle" data
-toggle
="collapse" data
-target
=".navbar-collapse">
<span
class="icon-bar"></span
>
<span
class="icon-bar"></span
>
<span
class="icon-bar"></span
>
</button
>
@Html
.ActionLink("通讯录", "Index", "user", new { area
= "" }, new { @
class = "navbar-brand" })
</div
>
<div
class="navbar-collapse collapse">
<ul
class="nav navbar-nav">
<li
class="dropdown">
<a href
="#" class="dropdown-toggle" data
-toggle
="dropdown">
用户管理
<b
class="caret"></b
>
</a
>
<ul
class="dropdown-menu">
<li
><a href
="/user/index">用户管理
</a
></li
>
<li
class="divider"></li
>
<li
><a href
="/user/add">用户添加
</a
></li
>
</ul
>
</li
>
<li
class="dropdown">
<a href
="#" class="dropdown-toggle" data
-toggle
="dropdown">
分组管理
<b
class="caret"></b
>
</a
>
<ul
class="dropdown-menu">
<li
><a href
="/group/index">分组管理
</a
></li
>
<li
class="divider"></li
>
<li
><a href
="/group/add">分组添加
</a
></li
>
</ul
>
</li
>
<li
class="dropdown">
<a href
="#" class="dropdown-toggle" data
-toggle
="dropdown">
通讯录管理
<b
class="caret"></b
>
</a
>
<ul
class="dropdown-menu">
<li
><a href
="/contact/index">通讯录管理
</a
></li
>
<li
class="divider"></li
>
<li
><a href
="/contact/add">通讯录添加
</a
></li
>
</ul
>
</li
>
</ul
>
</div
>
</div
>
</div
>
<div
class="container body-content">
@
RenderBody()
<hr
/>
<footer
>
<p
>©
; @DateTime
.Now
.Year
- 我的通讯录
</p
>
</footer
>
</div
>
@Scripts
.Render("~/bundles/jquery")
@Scripts
.Render("~/bundles/bootstrap")
@
RenderSection("scripts", required
: false)
</body
>
</html
>
5.2Error.cshtml
<!DOCTYPE html
>
<html
>
<head
>
<meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8"/>
<meta name
="viewport" content
="width=device-width" />
<title
>错误
</title
>
</head
>
<body
>
<hgroup
>
<h1
>错误。
</h1
>
<h2
>处理你的请求时出错。
</h2
>
</hgroup
>
</body
>
</html
>
总结
提示:以上就是通讯录的代码,需要源码可以在文章最顶部链接进行下载