日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

        歡迎訪問 生活随笔!

        生活随笔

        當前位置: 首頁 > 前端技术 > HTML >内容正文

        HTML

        Asp.net MVC 学习之路-003(增删改查,后端手工,前端生成)

        發布時間:2024/4/17 HTML 41 豆豆
        生活随笔 收集整理的這篇文章主要介紹了 Asp.net MVC 学习之路-003(增删改查,后端手工,前端生成) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

        時間:2017-03-09

        Asp.net Mvc實現增刪改查

        1, 創建數據庫

        可參考我寫的EntityFramework實現過程:http://www.jianshu.com/nb/10166743
        基于EF-ModelFirst思想在項目models文件夾下新建一個ADO.NET實體數據模型,完成實體和關系的建立,并生成數據庫。

        PIC001.png

        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方法上右擊鼠標選擇添加視圖
        模板選擇List,模型類選擇Student,上下文選擇你自己的上下文(你的上下文類名)。


        002.png

        自動生成:Views/Student/Index.cshtml

        將查詢結果作為集合通過ViewData.Model傳到前端

        #region 學生列表public ActionResult Index(){ViewData.Model = dbContext.StudentSet.AsEnumerable();//將查詢結果作為集合通過ViewData.Model傳到前端return View();} #endregion

        前端將通過foreach遍歷集合中的每個對象進行展示,具體可查看自動生成的代碼。~/Views/Student/Index.cshtml
        當前頁面(可先手動在數據庫添加一些數據):

        003.png


        在StudentController中添加方法

        #region Createpublic ActionResult Create(){return View();}

        通過Create這個方法新建頁面,方法同“查”。
        當前頁面:


        004.png

        加下來的問題是如何將前端的數據傳到后臺,并在后臺將數據保存到數據庫。
        點擊Create之后,系統會默認地去Controller中查找同名的Action進行處理(從哪來,回哪去),所以此時對Create方法進行重載,并且限制只有是HttpPost的時候進行響應。

        [HttpPost]public ActionResult Create(Student student) //從前端獲取對象{dbContext.StudentSet.Add(student);dbContext.SaveChanges();return RedirectToAction("index");//返回index頁面,即list頁面}

        此時點擊Edit


        005.png

        程序跳到另一個頁面,你們的程序會報錯,因為StudentController下面還沒有Edit方法,但是此時我們關注的對象是URL地址,通過URL地址我們可以看出index頁面是向Edit頁面傳遞了一個id,所以我們的Edit頁面可以通過這個id來獲取這個id對應的在數據庫中的值并進行展示。之后再進行更改。


        006.png

        在StudentController下面新建Edit方法

        public ActionResult Edit(int id){//ViewData.Model = dbContext.StudentSet.FirstOrDefault(u => u.Id == id);ViewData.Model = dbContext.StudentSet.Find(id);//將查詢結果通過ViewData.Model傳到前端return View();}

        現在點擊Edit之后已經能夠在Edit頁面對數據進行展示了,接下來的問題是如何將用戶更改后的數據同步到數據庫。
        思想同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");//使用這個方法會報錯 return Redirect("index");}

        Delete & Details

        #region Detailspublic ActionResult Details(int id){ViewData.Model = dbContext.StudentSet.Find(id);return View();}#endregion#region Deletepublic 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的增刪改查已全部實現。
        項目完整代碼:鏈接:http://pan.baidu.com/s/1i431ra1 密碼:5kjh

        與50位技術專家面對面20年技術見證,附贈技術全景圖

        總結

        以上是生活随笔為你收集整理的Asp.net MVC 学习之路-003(增删改查,后端手工,前端生成)的全部內容,希望文章能夠幫你解決所遇到的問題。

        如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。