ASP.NET MVC — 編輯 (Edit) 與檔案上傳 (FileUpload)

閱讀時間約 14 分鐘
微軟的範例,新增 (Create)有搭配檔案上傳
但沒有提供編輯 (Edit) 的功能
其實,以微軟提供的範例來說,這些功能都有了
“拼湊”都能做到您想要的功能。
==============================
因為微軟改為ASP.NET Core版的MVC
所以 原本範例 (PhotoSharing) 只能從網友留下的 “備份”去找
我手邊還有 MVC 4 與 MVC 5的版本,但內容大同小異。上面的範例就夠用了。
public ActionResult Edit_NEW(int id = 0)
 {
 Photo photo = _db.Photos.Find(id);
 if (photo == null)
 {
 return HttpNotFound(); // 找不到
 }
 return View(photo);
 }
接下來的 Edit_NEW檢視畫面中,要將 Photo資料表裡面的二進位內容(放置圖片的欄位,資料型態為 VarBinary(MAX)),還原成「圖片檔」,
這段程式在 Details檢視畫面 就有
(上傳以後,把圖片存入資料表。
所以,呈現圖片時,必須寫一段程式,把二進位內容,還原成圖片檔))
@model PhotoSharingApplication.Models.Photo
@{
 Layout = null;
}
<!DOCTYPE html>
<html>
<head>
 <meta name=”viewport” content=”width=device-width” />
 <title>Edit(原本範例沒有,這是我們額外增添的功能)</title>
</head>
<body>
 <script src=”~/Scripts/jquery-3.3.1.min.js”></script>
 <script src=”~/Scripts/jquery.validate.min.js”></script>
 <script src=”~/Scripts/jquery.validate.unobtrusive.min.js”></script>
<! — (1) 這一段程式,請從「Create檢視畫面」取得。
 因為要修改既有的圖片,想更新一張新圖片! →
 @using (Html.BeginForm(“Edit”, “Photo”, FormMethod.Post, new { enctype = “multipart/form-data” }))
 {
 @Html.AntiForgeryToken()
<div class=”form-horizontal”>
 <h4>Photo</h4>
<hr /> <! — (2) 隱藏欄位! 因為不允許人家修改「文章編號」,這是獨一無二的! →
 @Html.ValidationSummary(true, “”, new { @class = “text-danger” })
 @Html.HiddenFor(model => model.PhotoID)
<! — (3) 這一段程式,請從「Details檢視畫面」取得。 →
 @if (Model.PhotoFile != null)
 {
 <div class=”photo-display”>
 <img width=”800" src=”@Url.Action(“GetImage”, “Photo”, new { Model.PhotoID })” />
 <! — 取出圖片檔案 →
 </div>
 }
 <hr />
<div class=”form-group”>
 @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = “control-label col-md-2” })
 <div class=”col-md-10">
 @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = “form-control” } })
 @Html.ValidationMessageFor(model => model.Title, “”, new { @class = “text-danger” })
 </div>
 </div>
<! — (4) 這一段程式,請從「Create檢視畫面」取得。 →
 <div class=”form-group”>
 Upload new @Html.LabelFor(model => model.PhotoFile):
 <div class=”col-md-10">
 <! — **** 檔案上傳 **** →
 <input class=”pic-upload-input” type=”file” name=”Image”>
 <! — **** 檔案上傳 **** →
 </div>
 </div>
<div class=”form-group”>
 @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = “control-label col-md-2” })
 <div class=”col-md-10">
 @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = “form-control” } })
 @Html.ValidationMessageFor(model => model.Description, “”, new { @class = “text-danger” })
 </div>
 </div>
<! — (5) 這一段程式,請修改 ViewModel,也就是 /Models/Photo.cs的日期格式為 yyyy/MM/dd。 →
 <div class=”form-group”>
 @Html.LabelFor(model => model.CreatedDate, htmlAttributes: new { @class = “control-label col-md-2” })
 <div class=”col-md-10">
 @Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { @class = “form-control” } })
 @Html.ValidationMessageFor(model => model.CreatedDate, “”, new { @class = “text-danger” })
 </div>
 </div>
<div class=”form-group”>
 @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = “control-label col-md-2” })
 <div class=”col-md-10">
 @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = “form-control” } })
 @Html.ValidationMessageFor(model => model.UserName, “”, new { @class = “text-danger” })
 </div>
 </div>
<! — (6) 這一段程式,請修改 ViewModel,也就是 /Models/Photo.cs的日期格式為 yyyy/MM/dd。 →
 <div class=”form-group”>
 @Html.LabelFor(model => model.ModifiedDate, htmlAttributes: new { @class = “control-label col-md-2” })
 <div class=”col-md-10">
 @Html.EditorFor(model => model.ModifiedDate, new { htmlAttributes = new { @class = “form-control” } })
 @Html.ValidationMessageFor(model => model.ModifiedDate, “”, new { @class = “text-danger” })
 </div>
 </div>
<div class=”form-group”>
 <div class=”col-md-offset-2 col-md-10">
 <input type=”submit” value=”Save” class=”btn btn-default” />
 </div>
 </div>
</div>
 }
<div>
 @Html.ActionLink(“Back to List”, “Index”)
 </div>
</body>
</html>
按下 Submit按鈕以後,交給下一個動作來處理
==================================
//
 // POST: /Photo/Edit/5 (原本範例沒有,這是我們額外增添的功能)
 [HttpPost]
 [ValidateAntiForgeryToken] // 避免CSRF攻擊 
 public ActionResult Edit(Photo _photo, HttpPostedFileBase image)
 { // ****************************
 if (ModelState.IsValid) { 
 //*** 檔案上傳 ****************************************(start) 這段程式在 Create動作裡面也有
 if (image != null)
 {
 _photo.ImageMimeType = image.ContentType;
_photo.PhotoFile = new byte[image.ContentLength];
 image.InputStream.Read(_photo.PhotoFile, 0, image.ContentLength);
 }
 //*** 檔案上傳 ****************************************(end)
// 第一種寫法:
 _db.Entry(_photo).State = System.Data.Entity.EntityState.Modified; //確認被修改(狀態:Modified)
 _db.SaveChanges();
//return Content(“ 更新一筆記錄,成功!”); // 更新成功後,出現訊息(字串)。
 return RedirectToAction(“Index”);
 }
 else {
 return Content(“ *** 更新失敗!!*** “); 
 }

 }
ASP.NET MVC 線上相簿 (PhotoSharing) — 線上教學影片 https://9vs1.com/go/?i=4db77e0bf891
單獨購買本課程,優惠折扣 4BbKKha
連同「ASP.NET入門三天 (2500元)」「從入門到進階 (1800元)」「線上相簿 (3800元)」三門課一起買,優惠最多!!

課程適合對象

  • 本課程屬於進階課程,建議完成「ASP.NET MVC 教學 — 由零開始的入門課」課程後再來學習。 ** 本課程「不」適合從零開始的初學者 **
  • 需要撰寫網頁系統、或對於網頁與資料庫的開發有興趣的朋友。
  • 未來的程式設計師 — 希望將來進入業界寫程式,開發網頁系統。
  • 曾寫過 ASP、ASP.NET(Web Form)、PHP、JSP 的朋友,現在想學習 ASP.NET MVC 5。
為什麼會看到廣告
    4會員
    115Content count
    留言0
    查看全部
    發表第一個留言支持創作者!
    兩千MIS的沙龍 的其他內容
    課程介紹 https://9vs1.com/go/?i=3580aaf8f818 課程中還會分享DLL類別庫專案的作法,把共用的程式抽離出來。 最後更提供兩個實用的範例: 第一,以雜湊運算「單向」的打亂機密資訊。 第二,會員註冊以後,需要透過E-Mail再次確認身份,避免幽靈帳號大量註冊。
    新課程上架,(限時)折扣代碼 nFGAIK4 ASP.NET (MVC 5 + Core雙版本) — Repository倉庫與Interface介面 https://9vs1.com/go/?i=3061493ef814 課程介紹 此為進階課程,難度雖不高, 1.觀念說明與範例應用的情境
    [線上課程] ASP.NET MVC 5 + WebAPI + 前端特效(RWD / jQueryUI / FancyBox / CKeditor / Google Chart) 免費試聽 第一天(5.5小時)完整課程! 或是 **姓名: **E-Mail: 免費試聽第一天(5.5小時)完整課程!
    Microsoft.Data.SqlClient 與 System.Data.SqlClient 這篇文章是由SqlClient和SQL Server Tools的專案經理Vicky Harp寫的。簡介如下: 如果您使用ORM相關軟體,請注意: 長期目標是合併代碼庫,但我們目前還沒做到。
    讀了幾年書,人生也到了二三十歲 自己喜歡什麼?適合什麼?卻還沒有定論…..這樣是不是蹉跎了很多青春?? 有一位朋友看了Web Form第一天上課的影片,覺得都跟書本講得一樣。他自己看就好了。 就申請全額退費。我也不囉唆。 大約隔了一個半月,他又來了。 他說他想繳費,繼續看影片學習……
    無須基礎,只要「複製 + 貼上」就能學會 (RWD Bootstrap + jQueryUI + fancyBox + CKeditor + Google Chart) 每個特效均為您示範 WebFomr + MVC 雙重範例 ASP.NET 教學 — 前端特效輕鬆學 (9.9小時) 課程介紹
    課程介紹 https://9vs1.com/go/?i=3580aaf8f818 課程中還會分享DLL類別庫專案的作法,把共用的程式抽離出來。 最後更提供兩個實用的範例: 第一,以雜湊運算「單向」的打亂機密資訊。 第二,會員註冊以後,需要透過E-Mail再次確認身份,避免幽靈帳號大量註冊。
    新課程上架,(限時)折扣代碼 nFGAIK4 ASP.NET (MVC 5 + Core雙版本) — Repository倉庫與Interface介面 https://9vs1.com/go/?i=3061493ef814 課程介紹 此為進階課程,難度雖不高, 1.觀念說明與範例應用的情境
    [線上課程] ASP.NET MVC 5 + WebAPI + 前端特效(RWD / jQueryUI / FancyBox / CKeditor / Google Chart) 免費試聽 第一天(5.5小時)完整課程! 或是 **姓名: **E-Mail: 免費試聽第一天(5.5小時)完整課程!
    Microsoft.Data.SqlClient 與 System.Data.SqlClient 這篇文章是由SqlClient和SQL Server Tools的專案經理Vicky Harp寫的。簡介如下: 如果您使用ORM相關軟體,請注意: 長期目標是合併代碼庫,但我們目前還沒做到。
    讀了幾年書,人生也到了二三十歲 自己喜歡什麼?適合什麼?卻還沒有定論…..這樣是不是蹉跎了很多青春?? 有一位朋友看了Web Form第一天上課的影片,覺得都跟書本講得一樣。他自己看就好了。 就申請全額退費。我也不囉唆。 大約隔了一個半月,他又來了。 他說他想繳費,繼續看影片學習……
    無須基礎,只要「複製 + 貼上」就能學會 (RWD Bootstrap + jQueryUI + fancyBox + CKeditor + Google Chart) 每個特效均為您示範 WebFomr + MVC 雙重範例 ASP.NET 教學 — 前端特效輕鬆學 (9.9小時) 課程介紹
    你可能也想看
    Thumbnail
    重點摘要: 1.9 月降息 2 碼、進一步暗示年內還有 50 bp 降息 2.SEP 上修失業率預期,但快速的降息速率將有助失業率觸頂 3.未來幾個月經濟數據將繼續轉弱,經濟復甦的時點或是 1Q25 季底附近
    Thumbnail
    近期的「貼文發佈流程 & 版型大更新」功能大家使用了嗎? 新版式整體視覺上「更加凸顯圖片」,為了搭配這次的更新,我們推出首次貼文策展 ❤️ 使用貼文功能並完成這次的指定任務,還有機會獲得富士即可拍,讓你的美好回憶都可以用即可拍珍藏!
    Thumbnail
    重點摘要: 1.9 月降息 2 碼、進一步暗示年內還有 50 bp 降息 2.SEP 上修失業率預期,但快速的降息速率將有助失業率觸頂 3.未來幾個月經濟數據將繼續轉弱,經濟復甦的時點或是 1Q25 季底附近
    Thumbnail
    近期的「貼文發佈流程 & 版型大更新」功能大家使用了嗎? 新版式整體視覺上「更加凸顯圖片」,為了搭配這次的更新,我們推出首次貼文策展 ❤️ 使用貼文功能並完成這次的指定任務,還有機會獲得富士即可拍,讓你的美好回憶都可以用即可拍珍藏!