/// <summary>
/// 查詢數(shù)據(jù)庫(kù)中學(xué)生姓名
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
//使用linq,查詢數(shù)據(jù)上下文中的學(xué)生姓名
List<Models.T_student> list = (from d in db.T_student select d).ToList();
//將集合數(shù)據(jù)傳給視圖
ViewData["DataList"] = list;
return View();
} <span style="white-space:pre"> </span>/// <summary>
/// 根據(jù)學(xué)生ID刪除學(xué)生
/// </summary>
/// <param name="id">學(xué)生ID</param>
/// <returns></returns>
public ActionResult Del(string id)
{
//創(chuàng)建要?jiǎng)h除的實(shí)體,并將ID賦值給實(shí)體對(duì)象
T_student modelDel = new T_student() { studentId = id };
//將實(shí)體對(duì)象添加到EF管理容器
db.T_student.Attach(modelDel);
//將實(shí)體對(duì)象包裝類標(biāo)示為刪除狀態(tài)
db.T_student.Remove(modelDel);
//更新數(shù)據(jù)庫(kù)
db.SaveChanges();
//更新成功,跳轉(zhuǎn)到Index
return RedirectToAction("Index","MyClass");}#region 顯示要修改的數(shù)據(jù)
[HttpGet]
/// <summary>
/// 顯示要修改的數(shù)據(jù)
/// </summary>
/// <param name="id">要修改的學(xué)生ID</param>
/// <returns></returns>
public ActionResult Modify(string id)
{
//根據(jù)學(xué)生ID,查詢數(shù)據(jù)庫(kù),返回集合中拿到第一個(gè)實(shí)體對(duì)象
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,將數(shù)據(jù)傳給視圖上名為model的屬性
return View(ts);
}
#endregion
#region 保存要修改的數(shù)據(jù)
[HttpPost]
/// <summary>
/// 保存要修改的數(shù)據(jù)
/// </summary>
/// <param name="id">要修改的學(xué)生ID</param>
/// <returns></returns>
public ActionResult Modify(T_student ts)
{
//將實(shí)體對(duì)象加入EF對(duì)象容器中,并獲取包裝類對(duì)象
DbEntityEntry<T_student> entry=db.Entry<T_student>(ts);
//將包裝類設(shè)置為unchange
entry.State = System.Data.EntityState.Unchanged;
//設(shè)置被改變的屬性
entry.Property(a=>a.studentName).IsModified=true;
entry.Property(a => a.classId).IsModified = true;
//提交更新到數(shù)據(jù)庫(kù)
db.SaveChanges();
//更新成功,跳轉(zhuǎn)到Index
return RedirectToAction("Index", "MyClass");
}
#endregion
3.添加查詢列表視圖(Index.cshtml)
@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方法 設(shè)置viewData的集合數(shù)據(jù)生成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)
十載專注成都網(wǎng)站制作,
企業(yè)網(wǎng)站制作,個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于
企業(yè)網(wǎng)站制作,高端網(wǎng)頁(yè)制作,對(duì)
成都混凝土攪拌罐等多個(gè)領(lǐng)域,擁有豐富的網(wǎng)站維護(hù)經(jīng)驗(yàn)。@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獲取數(shù)據(jù)賦值給下拉框-->
<td>@Html.DropDownListFor(a => a.classId, ViewBag.classList as IEnumerable<SelectListItem>)</td>
</tr>
<tr>
<td>學(xué)生姓名</td>
<!--使用HtmlHepler,直接從model獲取數(shù)據(jù)賦值給文本框-->
<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>
分享文章:mvc+linq+EF對(duì)數(shù)據(jù)表的查刪改-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article22/eicjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站排名、App設(shè)計(jì)、外貿(mào)建站、域名注冊(cè)
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源:
創(chuàng)新互聯(lián)