首页 > 技术文章 > MVC4学习笔记(一)

dong897812629 2014-02-20 23:02 原文

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

 

 

推荐阅读