MVC4學習筆記(一)

1.查詢

   1)Controllersjavascript

         /// <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)Viewjava

@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>
   }
相關文章
相關標籤/搜索