時(shí)間:2017-03-09
Asp.net Mvc實(shí)現(xiàn)增刪改查
1便锨, 創(chuàng)建數(shù)據(jù)庫
可參考我寫的EntityFramework實(shí)現(xiàn)過程:http://www.reibang.com/nb/10166743
基于EF-ModelFirst思想在項(xiàng)目models文件夾下新建一個(gè)ADO.NET實(shí)體數(shù)據(jù)模型围辙,完成實(shí)體和關(guān)系的建立,并生成數(shù)據(jù)庫放案。
2姚建,新建控制器
新建StudentController
一般index頁面用來展示列表
using System.Web;
using System.Web.Mvc;
using MVCFirstDemo.Models;
namespace MVCFirstDemo.Controllers
{
public class StudentController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
在index方法上右擊鼠標(biāo)選擇添加視圖
模板選擇List,模型類選擇Student吱殉,上下文選擇你自己的上下文(你的上下文類名)掸冤。
自動(dòng)生成:Views/Student/Index.cshtml
查
將查詢結(jié)果作為集合通過ViewData.Model傳到前端
#region 學(xué)生列表
public ActionResult Index()
{
ViewData.Model = dbContext.StudentSet.AsEnumerable();//將查詢結(jié)果作為集合通過ViewData.Model傳到前端
return View();
}
#endregion
前端將通過foreach遍歷集合中的每個(gè)對(duì)象進(jìn)行展示,具體可查看自動(dòng)生成的代碼友雳。~/Views/Student/Index.cshtml
當(dāng)前頁面(可先手動(dòng)在數(shù)據(jù)庫添加一些數(shù)據(jù)):
003.png
增
在StudentController中添加方法
#region Create
public ActionResult Create()
{
return View();
}
通過Create這個(gè)方法新建頁面稿湿,方法同“查”。
當(dāng)前頁面:
加下來的問題是如何將前端的數(shù)據(jù)傳到后臺(tái)押赊,并在后臺(tái)將數(shù)據(jù)保存到數(shù)據(jù)庫饺藤。
點(diǎn)擊Create之后,系統(tǒng)會(huì)默認(rèn)地去Controller中查找同名的Action進(jìn)行處理(從哪來流礁,回哪去)策精,所以此時(shí)對(duì)Create方法進(jìn)行重載,并且限制只有是HttpPost的時(shí)候進(jìn)行響應(yīng)崇棠。
[HttpPost]
public ActionResult Create(Student student) //從前端獲取對(duì)象
{
dbContext.StudentSet.Add(student);
dbContext.SaveChanges();
return RedirectToAction("index");//返回index頁面咽袜,即list頁面
}
改
此時(shí)點(diǎn)擊Edit
程序跳到另一個(gè)頁面,你們的程序會(huì)報(bào)錯(cuò)枕稀,因?yàn)镾tudentController下面還沒有Edit方法询刹,但是此時(shí)我們關(guān)注的對(duì)象是URL地址,通過URL地址我們可以看出index頁面是向Edit頁面?zhèn)鬟f了一個(gè)id萎坷,所以我們的Edit頁面可以通過這個(gè)id來獲取這個(gè)id對(duì)應(yīng)的在數(shù)據(jù)庫中的值并進(jìn)行展示凹联。之后再進(jìn)行更改。
在StudentController下面新建Edit方法
public ActionResult Edit(int id)
{
//ViewData.Model = dbContext.StudentSet.FirstOrDefault(u => u.Id == id);
ViewData.Model = dbContext.StudentSet.Find(id);//將查詢結(jié)果通過ViewData.Model傳到前端
return View();
}
現(xiàn)在點(diǎn)擊Edit之后已經(jīng)能夠在Edit頁面對(duì)數(shù)據(jù)進(jìn)行展示了哆档,接下來的問題是如何將用戶更改后的數(shù)據(jù)同步到數(shù)據(jù)庫蔽挠。
思想同Create,從哪來,到哪去澳淑。
[HttpPost]
public ActionResult Edit(Student student)
{
dbContext.Entry(student).State = System.Data.Entity.EntityState.Modified;
dbContext.SaveChanges();
return RedirectToAction("Index");
//連接到其他控制器下的方法 return RedirectToAction("Index","Home");
//使用這個(gè)方法會(huì)報(bào)錯(cuò) return Redirect("index");
}
Delete & Details
#region Details
public ActionResult Details(int id)
{
ViewData.Model = dbContext.StudentSet.Find(id);
return View();
}
#endregion
#region Delete
public ActionResult Delete(int id)
{
ViewData.Model = dbContext.StudentSet.Find(id);
return View();
}
[HttpPost]
public ActionResult Delete(Student student)
{
dbContext.Entry(student).State = System.Data.Entity.EntityState.Deleted;
dbContext.SaveChanges();
return RedirectToAction("Index");
}
#endregion
至此Asp.net Mvc的增刪改查已全部實(shí)現(xiàn)比原。
項(xiàng)目完整代碼:鏈接:http://pan.baidu.com/s/1i431ra1 密碼:5kjh