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

2019/08/26閱讀時間約 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。
為什麼會看到廣告
    2會員
    113內容數
    留言0
    查看全部
    發表第一個留言支持創作者!