数据表:ls_help_type 数据库模型:ZYXModel
Controller:
ZYXModel.DataEntities db = new ZYXModel.DataEntities();//数据上下文对象
******************************Index控制器:查询数据列表
/// <summary>
/// 问题类型首页(查)
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
List<ZYXModel.ls_help_type> list = (from d in db.ls_help_type select d).ToList();
//使用ViewData将数据传给View
ViewData["DataList"] = list;
return View();
}
******************************Index对应视图:
<div id="nav" class="tc"><a href="/Admin//Helps/Index" class="navn">问题类型管理</a><a href="/Admin/Helps/queIndex" class="nava">问题管理</a></div>
<div id="main">
<table class="table" style="text-align:center ">
<!--要取到数据就直接@对应的变量 这样就可以了(导入命名空间也是一个道理)-->
<tr>
<th>编号</th>
<th>问题类型</th>
<th>操作</th>
</tr>
@foreach (ZYXModel.ls_help_type a in ViewData["DataList"] as List<ZYXModel.ls_help_type>)
{
<tr>
<td>@a.id</td>
<td>@a.helpDescribe</td>
<td>
<a href="/Admin/Helps/Modify/@a.id">修改</a>
<a href="/Admin/Helps/Del/@a.id">删除</a>
</td>
</tr>
}
<tr><td></td><td></td><td><a href="/Admin/Helps/AddHelType">增加</a></td></tr>
</table>
/// <summary>
/// 增加问题类型,get过来调用此控制器(增)
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult AddHelType()
{
return View();
}
******************************[HttpGet]AddHtlType对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("AddHelType", "Helps", FormMethod.Post))
{
<table>
<tr style="text-align:center">
<td colspan="2">增加</td>
</tr>
<tr>
<td>问题类型:</td>
<td><input type="text" name="helType"</td>
<td><input type="submit" value="确定增加" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
}
******************************Post过来的AddHelType控制器:增加数据
/// <summary>
/// 增加问题类型,post过来调用此控制器(增)
/// </summary>
/// <param name="borrowId"></param>
/// <returns></returns>
[HttpPost]
public ActionResult AddHelType(string helType)
{
string helpTypeNmae = helType ;
string sql = "insert into ls_help_type (helpDescribe) values('" + helpTypeNmae + "');";
int lines = SqlHelper.ExecuteNonQuery(sql);//执行操作,返回受影响的行数
if (lines >= 1)
{//数据插入成功,返回前台json对象
return RedirectToAction("Index", "Helps");
}
else
{//数据插入失败,返回前台json对象
return RedirectToAction("Index", "Error");
}
}
******************************[HttpPost]AddHtlType对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("AddHelType", "Helps", FormMethod.Post))
{
<table>
<tr style="text-align:center">
<td colspan="2">增加</td>
</tr>
<tr>
<td>问题类型:</td>
<td><input type="text" name="helType"</td>
<td><input type="submit" value="确定增加" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
/// <summary>
/// 修改问题类型,get过来调用此控制器(改)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public ActionResult Modify(int id)
{
ZYXModel.ls_help_type art = (from d in db.ls_help_type where d.id == id select d).FirstOrDefault();
//将数据传递给视图:用ViewBag viewData 使用view的构造函数
return View(art);
}
******************************[HttpGet]Modify对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("Modify", "Helps", FormMethod.Post))
{
<table>
<tr>
<td colspan="2">修改</td>
</tr>
<tr>
<td>问题类型:@Html.HiddenFor(a=>a.id)</td>
@*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@
<!--使用htmlHelper的强类型方法,直接从Model中根据id生成文本框-->
<td>@Html.TextBoxFor(a =>a.helpDescribe)</td>
<td><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
}
******************************post过来的Modify控制器:修改对应数据
/// <summary>
/// 修改问题类型,post过来调用此控制器(改)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Modify(ZYXModel.ls_help_type model)
{
try
{
//1,将实体对象加入EF对象容器中,并获取伪包装类对象
DbEntityEntry<ZYXModel.ls_help_type> entry = db.Entry<ZYXModel.ls_help_type>(model);
//2,将伪包装类对象的状态设置为unchanged
entry.State = System.Data.Entity.EntityState.Unchanged;
//3,设置被改变的属性
entry.Property(a => a.helpDescribe).IsModified = true;
//4,提交到数据库 完成修改
db.SaveChanges();
return RedirectToAction("Index", "Helps");
}
catch (Exception)
{
return RedirectToAction("Test", "Test");
}
}
******************************[HttpGet]Modify对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("Modify", "Helps", FormMethod.Post))
{
<table>
<tr>
<td colspan="2">修改</td>
</tr>
<tr>
<td>问题类型:@Html.HiddenFor(a=>a.id)</td>
@*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@
<!--使用htmlHelper的强类型方法,直接从Model中根据id生成文本框-->
<td>@Html.TextBoxFor(a =>a.helpDescribe)</td>
<td><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
/// <summary>
/// 删除问题类型(删)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Del(int id) //这个id是通过超链接带过来的
{
try
{
//需要一个实体对象参数
//db.Customers.Remove(new Customer() {CustomerNo = id });
//1,创建要删除的对象
ZYXModel.ls_help_type modelDel = new ZYXModel.ls_help_type() { id = id };
//2,将对象添加到EF管理容器中
db.ls_help_type.Attach(modelDel);
//3,修改对象的包装类对象标识为删除状态
db.ls_help_type.Remove(modelDel);
//4,更新到数据库
db.SaveChanges();
//5,更新成功,则命令流浪器重定向 到 /Home/Index 方法
return RedirectToAction("Index", "Helps");
}
catch (Exception)
{
//指定对应跳转的视图Test下的Test.cshtml文件
return RedirectToAction("Index", "Error");
//return Content("删除失败" + ex.Message);
}
}
Controller:
ZYXModel.DataEntities db = new ZYXModel.DataEntities();//数据上下文对象
******************************Index控制器:查询数据列表
/// <summary>
/// 问题类型首页(查)
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
List<ZYXModel.ls_help_type> list = (from d in db.ls_help_type select d).ToList();
//使用ViewData将数据传给View
ViewData["DataList"] = list;
return View();
}
******************************Index对应视图:
<div id="nav" class="tc"><a href="/Admin//Helps/Index" class="navn">问题类型管理</a><a href="/Admin/Helps/queIndex" class="nava">问题管理</a></div>
<div id="main">
<table class="table" style="text-align:center ">
<!--要取到数据就直接@对应的变量 这样就可以了(导入命名空间也是一个道理)-->
<tr>
<th>编号</th>
<th>问题类型</th>
<th>操作</th>
</tr>
@foreach (ZYXModel.ls_help_type a in ViewData["DataList"] as List<ZYXModel.ls_help_type>)
{
<tr>
<td>@a.id</td>
<td>@a.helpDescribe</td>
<td>
<a href="/Admin/Helps/Modify/@a.id">修改</a>
<a href="/Admin/Helps/Del/@a.id">删除</a>
</td>
</tr>
}
<tr><td></td><td></td><td><a href="/Admin/Helps/AddHelType">增加</a></td></tr>
</table>
</div>
/// <summary>
/// 增加问题类型,get过来调用此控制器(增)
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult AddHelType()
{
return View();
}
******************************[HttpGet]AddHtlType对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("AddHelType", "Helps", FormMethod.Post))
{
<table>
<tr style="text-align:center">
<td colspan="2">增加</td>
</tr>
<tr>
<td>问题类型:</td>
<td><input type="text" name="helType"</td>
<td><input type="submit" value="确定增加" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
}
******************************Post过来的AddHelType控制器:增加数据
/// <summary>
/// 增加问题类型,post过来调用此控制器(增)
/// </summary>
/// <param name="borrowId"></param>
/// <returns></returns>
[HttpPost]
public ActionResult AddHelType(string helType)
{
string helpTypeNmae = helType ;
string sql = "insert into ls_help_type (helpDescribe) values('" + helpTypeNmae + "');";
int lines = SqlHelper.ExecuteNonQuery(sql);//执行操作,返回受影响的行数
if (lines >= 1)
{//数据插入成功,返回前台json对象
return RedirectToAction("Index", "Helps");
}
else
{//数据插入失败,返回前台json对象
return RedirectToAction("Index", "Error");
}
}
******************************[HttpPost]AddHtlType对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("AddHelType", "Helps", FormMethod.Post))
{
<table>
<tr style="text-align:center">
<td colspan="2">增加</td>
</tr>
<tr>
<td>问题类型:</td>
<td><input type="text" name="helType"</td>
<td><input type="submit" value="确定增加" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
}
/// <summary>
/// 修改问题类型,get过来调用此控制器(改)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public ActionResult Modify(int id)
{
ZYXModel.ls_help_type art = (from d in db.ls_help_type where d.id == id select d).FirstOrDefault();
//将数据传递给视图:用ViewBag viewData 使用view的构造函数
return View(art);
}
******************************[HttpGet]Modify对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("Modify", "Helps", FormMethod.Post))
{
<table>
<tr>
<td colspan="2">修改</td>
</tr>
<tr>
<td>问题类型:@Html.HiddenFor(a=>a.id)</td>
@*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@
<!--使用htmlHelper的强类型方法,直接从Model中根据id生成文本框-->
<td>@Html.TextBoxFor(a =>a.helpDescribe)</td>
<td><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
}
******************************post过来的Modify控制器:修改对应数据
/// <summary>
/// 修改问题类型,post过来调用此控制器(改)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Modify(ZYXModel.ls_help_type model)
{
try
{
//1,将实体对象加入EF对象容器中,并获取伪包装类对象
DbEntityEntry<ZYXModel.ls_help_type> entry = db.Entry<ZYXModel.ls_help_type>(model);
//2,将伪包装类对象的状态设置为unchanged
entry.State = System.Data.Entity.EntityState.Unchanged;
//3,设置被改变的属性
entry.Property(a => a.helpDescribe).IsModified = true;
//4,提交到数据库 完成修改
db.SaveChanges();
return RedirectToAction("Index", "Helps");
}
catch (Exception)
{
return RedirectToAction("Test", "Test");
}
}
******************************[HttpGet]Modify对应视图:
@model ZYXModel.ls_help_type
@using (Html.BeginForm("Modify", "Helps", FormMethod.Post))
{
<table>
<tr>
<td colspan="2">修改</td>
</tr>
<tr>
<td>问题类型:@Html.HiddenFor(a=>a.id)</td>
@*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@
<!--使用htmlHelper的强类型方法,直接从Model中根据id生成文本框-->
<td>@Html.TextBoxFor(a =>a.helpDescribe)</td>
<td><input type="submit" value="确定修改" />@Html.ActionLink("返回","Index","Helps")</td>
</tr>
</table>
}
/// <summary>
/// 删除问题类型(删)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Del(int id) //这个id是通过超链接带过来的
{
try
{
//需要一个实体对象参数
//db.Customers.Remove(new Customer() {CustomerNo = id });
//1,创建要删除的对象
ZYXModel.ls_help_type modelDel = new ZYXModel.ls_help_type() { id = id };
//2,将对象添加到EF管理容器中
db.ls_help_type.Attach(modelDel);
//3,修改对象的包装类对象标识为删除状态
db.ls_help_type.Remove(modelDel);
//4,更新到数据库
db.SaveChanges();
//5,更新成功,则命令流浪器重定向 到 /Home/Index 方法
return RedirectToAction("Index", "Helps");
}
catch (Exception)
{
//指定对应跳转的视图Test下的Test.cshtml文件
return RedirectToAction("Index", "Error");
//return Content("删除失败" + ex.Message);
}
}