mvc+linq+EF對數據表的查刪改

         /// <summary>
        /// 查詢數據庫中學生姓名
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            //使用linq,查詢數據上下文中的學生姓名
            List<Models.T_student> list = (from d in db.T_student select d).ToList();

            //將集合數據傳給視圖
            ViewData["DataList"] = list;
            return View();
        } 

  

         <span style="white-space:pre">	</span>/// <summary>
        /// 根據學生ID刪除學生
        /// </summary>
        /// <param name="id">學生ID</param>
        /// <returns></returns>
        public ActionResult Del(string id)
        {
            //建立要刪除的實體,並將ID賦值給實體對象
            T_student modelDel = new T_student() { studentId = id };

            //將實體對象添加到EF管理容器
            db.T_student.Attach(modelDel);

            //將實體對象包裝類標示爲刪除狀態
            db.T_student.Remove(modelDel);

            //更新數據庫
            db.SaveChanges();

            //更新成功,跳轉到Index
            return RedirectToAction("Index","MyClass");}

  

#region 顯示要修改的數據
    [HttpGet]
        /// <summary>
        /// 顯示要修改的數據
        /// </summary>
        /// <param name="id">要修改的學生ID</param>
        /// <returns></returns>
        public ActionResult Modify(string id)
        {
            //根據學生ID,查詢數據庫,返回集合中拿到第一個實體對象
            T_student ts = (from a in db.T_student where a.studentId == id select a).FirstOrDefault();

            //查詢課程名稱
            IEnumerable<SelectListItem> listItem=(from c in db.T_class select c).ToList().Select(c=>new SelectListItem{Value=c.classId.ToString(),Text=c.className});
            
            //查詢到的課程名稱給Viewbag
            ViewBag.classList = listItem;

            //使用View,將數據傳給視圖上名爲model的屬性
            return View(ts);
        } 
        #endregion


        #region 保存要修改的數據
        [HttpPost]
        /// <summary>
        /// 保存要修改的數據
        /// </summary>
        /// <param name="id">要修改的學生ID</param>
        /// <returns></returns>
        public ActionResult Modify(T_student ts)
        {
            //將實體對象加入EF對象容器中,並獲取包裝類對象
            DbEntityEntry<T_student> entry=db.Entry<T_student>(ts);

            //將包裝類設置爲unchange
            entry.State = System.Data.EntityState.Unchanged;

            //設置被改變的屬性
            entry.Property(a=>a.studentName).IsModified=true;
            entry.Property(a => a.classId).IsModified = true;

            //提交更新到數據庫
            db.SaveChanges();

            //更新成功,跳轉到Index
            return RedirectToAction("Index", "MyClass");
        }
        #endregion

   3.添加查詢列表視圖(Index.cshtml)css

@using MyMvcTest.Models
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        #tblist {
            border:1px solid #0094ff;
            width:600px;
            margin:10px auto;
            border-collapse:collapse;
        }
            #tblist th, td {
                border:1px solid #0094ff;
                padding:10px;
            }
    </style>
</head>
<body>
    <table id="tblist">
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>課程ID</th>
            <th>編輯</th>
        </tr>
        <!--變量action方法 設置viewData的集合數據生成html-->
        @foreach (T_student student in ViewData["DataList"] as List<T_student>)
        {
        <tr>
            <td>@student.studentId</td>
            <td>@student.studentName</td>
            <td>@student.classId</td>
            <td>
                <a href="/MyClass/del/@student.studentId">刪除</a>
                <a href="/MyClass/modify/@student.studentId">修改</a>
            </td>
        </tr>
        }
    </table>

</body>
</html>

  添加「修改」視圖(modify.cshtml)html

@model MyMvcTest.Models.T_student
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Modify</title>
    <style type="text/css">
        #tblist {
            border: 1px solid #0094ff;
            width: 600px;
            margin: 10px auto;
            border-collapse: collapse;
        }

            #tblist th, td {
                border: 1px solid #0094ff;
                padding: 10px;
            }
    </style>
</head>
<body>
    @using (Html.BeginForm("Modify", "MyClass", FormMethod.Post))
{
    <table id="tblist">
        <tr>
            <td colspan="2">修改:@Html.HiddenFor(a=>a.studentId)</td>
        </tr>
        <tr>
            <td>課程名稱</td>
            <!--使用HtmlHepler,直接從model獲取數據賦值給下拉框-->
            <td>@Html.DropDownListFor(a => a.classId, ViewBag.classList as IEnumerable<SelectListItem>)</td>
        </tr>
        <tr>
            <td>學生姓名</td>
            <!--使用HtmlHepler,直接從model獲取數據賦值給文本框-->
            <td>@Html.TextBoxFor(a => a.studentName)</td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="肯定修改">@Html.ActionLink("返回", "Index", "MyClass")</td>
        </tr>
    </table>
}
</body>
</html>
相關文章
相關標籤/搜索