微軟的範例,新增 (Create)有搭配檔案上傳

 

 

但沒有提供編輯 (Edit) 的功能

其實,以微軟提供的範例來說,這些功能都有了

 

"拼湊"都能做到您想要的功能。

詳見 9vs1.com線上課程 -  https://9vs1.com/go/?i=4db77e0bf891(課程優惠代碼 4BbKKha​ )

 

=========================================================

因為微軟改為ASP.NET Core版的MVC

https://github.com/MicrosoftLearning/20486-DevelopingASPNETMVCWebApplications

 

所以 原本範例 (PhotoSharing) 只能從網友留下的 "備份"去找

https://github.com/faiteo/Photo-Sharing-Application-using-Asp.Net-mvc-4-/tree/master/PhotoSharingApplication-2015/PhotoSharingApplication-2015

 

我手邊還有 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檢視畫面 就有

(上傳以後,把圖片存入資料表。

    所以,呈現圖片時,必須寫一段程式,把二進位內容,還原成圖片檔))

 

詳見 9vs1.com線上課程 -  https://9vs1.com/go/?i=4db77e0bf891(課程優惠代碼 4BbKKha​ )

@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按鈕以後,交給下一個動作來處理

詳見 9vs1.com線上課程 -  https://9vs1.com/go/?i=4db77e0bf891(課程優惠代碼 4BbKKha​ )

=========================================================

        //
        // 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(" *** 更新失敗!!*** "); 
            }
            
        }

 

arrow
arrow

    MIS2000 Lab 發表在 痞客邦 留言(0) 人氣()