1.查询

1)Controllers

         /// <summary>
/// 数据上下文对象
/// </summary>
OumindBlogEntities db = new OumindBlogEntities(); #region 查询文章列表
/// <summary>
/// 查询文章列表
/// </summary>
/// <returns></returns>
public ActionResult Index()
{ //linq
List<Models.BlogArticle> list = (from d in db.BlogArticles where d.AIsDel == false select d).ToList();
//1.2将集合数据传给视图
ViewBag.DataList = list;
//ViewData["DataList"] = list;
return View();
} #endregion

2)View

@using MVCBlog.Models
<!--引入命名空间-->
<table id="tbList">
<tr>
<th>id</th>
<th>标题</th>
<th>分类</th>
<th>状态</th>
<th>时间</th>
<th>操作</th>
</tr>
@foreach (BlogArticle a in ViewBag.DataList as List<BlogArticle>)
{
<tr>
<td>@a.AId</td>
<td>@a.ATitle</td>
<td>@a.BlogArticleCate.Name</td>
<td>@a.Enumeration.e_cname</td>
<td>@a.AAddtime</td>
<td>
<a href="javascript:del(@a.AId)">删除</a>
<a href="/Home/Modify/@a.AId">修改</a>
</td>
</tr> }
</table>

2.删除

1)Controllers

     #region  删除操作
/// <summary>
/// 删除操作
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Del(int id)
{
try
{
//1.创建要删除的对象
BlogArticle modelDel = new BlogArticle { AId = id };
//2.将对象添加到 EF 管理容器
db.BlogArticles.Attach(modelDel);
//3.将对象包装类的状态 标识为 删除状态
db.BlogArticles.Remove(modelDel);
//4.更新到数据库
db.SaveChanges();
//5.更新成功,则命令浏览器 重定向到 /Home/List 方法
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return Content("删除失败~~~" + ex.Message);
} }
#endregion

3.修改

1)Controller

         using System.Data.Entity.Infrastructure;
#region 显示要修改的数据
/// <summary>
/// 显示要修改的数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Modify(int id)
{
BlogArticle model = (from a in db.BlogArticles where a.AId == id select a).FirstOrDefault(); //生成文章分类 下拉框 列表集合 <option value="1">文本</option>
IEnumerable<SelectListItem> listItem = (from c in db.BlogArticleCates
where c.IsDel == false
select c).ToList().Select(c => new SelectListItem { Value=c.Id.ToString(),Text=c.Name});
//将生成的文章分类 下来框 列表集合 设置给ViewBag
ViewBag.CateList = listItem; //使用View 构造函数,将实体传递给视图上名为Model的属性
return View(model); }
#endregion #region 0.5 执行修改
[HttpPost]
/// <summary>
/// 0.5 执行修改
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ActionResult Modify(BlogArticle model)
{
//1.将实体对象a 加入到EF 对象容器中,并 b获取 伪包装类 对象
DbEntityEntry<BlogArticle> entry = db.Entry<BlogArticle>(model);
//2.将包装类对象 的状态设置为 Unchanged
entry.State = System.Data.EntityState.Unchanged;
//3.设置 被改变的 属性
entry.Property(a => a.ATitle).IsModified = true;
entry.Property(a => a.AContent).IsModified = true;
entry.Property(a => a.ACate).IsModified = true;
//4.提交到数据库,完成修改
db.SaveChanges();
//5.命令 浏览器 重定向 到 Home/List 方法
return RedirectToAction("Index", "Home"); }
#endregion

2)View

  @model MVCBlog.Models.BlogArticle
<!--指定页面Model 属性 的类型-->
@using(Html.BeginForm("Modify","Home",FormMethod.Post))
{ <table id="tbList">
<tr><td colspan="2">修改:@Html.HiddenFor(a=>a.AId)</td></tr>
<tr>
<td>标题:</td>
<td>@Html.TextBoxFor(a=>a.ATitle)</td>
</tr>
<tr>
<td>分类:</td>
<!--使用强类型方法生成下拉框,并自动根据 model属性里的ACate值 设置 下拉框的默认选中项-->
<td>@Html.DropDownListFor(a => a.ACate, ViewBag.CateList as IEnumerable<SelectListItem>)</td>
</tr>
<tr>
<td>内容:</td>
<td>@Html.TextAreaFor(a=>a.AContent,10,60,null)</td>
</tr>
<tr>
<td colspan="2" ><input type="submit" value="确认修改"/>@Html.ActionLink("返回","Index","Home")</td>
</tr>
</table>
}

最新文章

  1. 手动为php安装memcached扩展模块
  2. java创建线程的几种方式
  3. B-tree/B+tree/B*tree [转]
  4. CF Amr and Pins (数学)
  5. JAVA多线程实现简单的点名系统
  6. Sass中的Map 详解
  7. codevs 1107 等价表达式
  8. 关于HTML5 语音搜索的问题
  9. React-Native post和get请求
  10. height/innerHeight/outerHeight
  11. NFC学习笔记2——Libnfc简介及安装
  12. 关于ZendStudio 10.5的破解
  13. 53. leetcode557. Reverse Words in a String III
  14. python中的类机制
  15. 基于百度AI开放平台的人脸识别及语音合成
  16. aiohttp AppRunner的用法
  17. Linux C 编程
  18. app与jvm 反向代理时config的设置(用于在web页面显示npm(就如tomcat)产生的页面)
  19. (动态规划)有 n 个学生站成一排,每个学生有一个能力值,从这 n 个学生中按照顺序选取kk 名学生,要求相邻两个学生的位置编号的差不超过 d,使得这 kk 个学生的能力值的乘积最大,返回最大的乘积
  20. 父级和 子集 controller 之间的通讯

热门文章

  1. Hadoop Shell命令字典(可收藏)
  2. 数据持久化之sqlite基本用法
  3. 边工作边刷题:70天一遍leetcode: day 88-5
  4. SQL 第一范式、第二范式、第三范式、BCNF
  5. 阻塞与非阻塞IO step by step
  6. FusionCharts或其它flash的div图层总是浮在最上层? (转)
  7. 【C#】窗体动画效果
  8. 【转】WCF与Web API 区别(应用场景)
  9. 给vps设置ssh供爬墙使用
  10. OV7725学习(二)