標籤:
寫在前面
上篇文章實現了檔案的下載,本篇文章將實現編輯檔案名稱的功能。
系列文章
[EF]vs15+ef6+mysql code first方式
[實戰]MVC5+EF6+MySql企業網盤實戰(1)
[實戰]MVC5+EF6+MySql企業網盤實戰(2)——使用者註冊
[實戰]MVC5+EF6+MySql企業網盤實戰(3)——驗證碼
[實戰]MVC5+EF6+MySql企業網盤實戰(4)——上傳頭像
[Bootstrap]modal彈出框
[實戰]MVC5+EF6+MySql企業網盤實戰(5)——登入介面,頭像等比例壓縮
[實戰]MVC5+EF6+MySql企業網盤實戰(5)——頁面模板
[實戰]MVC5+EF6+MySql企業網盤實戰(5)——ajax方式註冊
[實戰]MVC5+EF6+MySql企業網盤實戰(6)——ajax方式登入
[實戰]MVC5+EF6+MySql企業網盤實戰(7)——檔案上傳
[實戰]MVC5+EF6+MySql企業網盤實戰(8)——檔案下載、刪除
[實戰]MVC5+EF6+MySql企業網盤實戰(9)——編輯檔案名稱
核心代碼
前段代碼
<tbody role="alert" aria-live="polite" aria-relevant="all"> @{int i = 0;} @foreach (var item in Model) { i++; <tr class="i%2==0?‘even‘:‘odd‘" id="[email protected]"> <td class=" even sorting_1"><img src="@item.FileIcon" alt="" /> <span id="[email protected]">@item.FileName</span></td> <td class="center ">@item.FileSize 位元組</td> <td class="center ">@item.ModifyDt</td> <td class="center "> <a class="btn btn-success" href="/Home/[email protected]"> <i class="glyphicon glyphicon-zoom-in icon-white"></i> 下載 </a> <a class="btn btn-info" href="javascript:void(0)" onclick="editFile(@item.Id,‘@item.FileName‘)"> <i class="glyphicon glyphicon-edit icon-white"></i> 編輯 </a> <a class="btn btn-danger" href="javascript:void(0)" onclick="deleteFile(@item.Id)"> <i class="glyphicon glyphicon-trash icon-white"></i> 刪除 </a> </td> </tr> } </tbody>
模式彈出框
<div class="modal fade" id="modal-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h3>編輯</h3> </div> <div class="modal-body"> <input type="text" placeholder="請輸入名稱" class="form-control" name="name" value="" id="txtFileName" /> </div> <div class="modal-footer"> <a href="#" class="btn btn-default" data-dismiss="modal" id="lnkCancel">取消</a> <a href="#" class="btn btn-primary" data-dismiss="modal" id="lnkSave">儲存</a> </div> </div> </div></div>
請求
//編輯檔案名稱 function editFile(fileId, fileName) { $("#txtFileName").val(fileName); $("#modal-edit").modal(‘show‘); //彈出框註冊取消,儲存事件 $(‘#lnkCancel‘).click(function () { //單機取消,清空內容 $("#txtFileName").val(‘‘); }); $(‘#lnkSave‘).click(function () { var file = { fileId: fileId, fileNewName: $(‘#txtFileName‘).val() }; $.post("/Home/EditFileName", file, function (data) { data = JSON.parse(data); if (data.code == 200) { $(‘#sp-‘ + fileId).html(file.fileNewName); } }); }); };
服務端代碼
public JsonResult EditFileName() { string fileId=Request.Form["fileId"]; string fileNewName=Request.Form["fileNewName"]; UserInfo userInfo = Session["user"] as UserInfo; if (userInfo == null) { RedirectToAction("Login", "UserInfo"); } int id = Convert.ToInt32(fileId); var findFile = _myFileServiceRepository.Find(x => x.Id == id); findFile.FileName = fileNewName; _myFileServiceRepository.Update(findFile); int count = _myFileServiceRepository.SaveChanges(); if (count > 0) { var response = new { code = 200, msg = "更新成功" }; return new JsonResult() { Data = new JavaScriptSerializer().Serialize(response) }; } return new JsonResult() { Data = new JavaScriptSerializer().Serialize(new { code = 500, msg = "儲存失敗" }) }; }
測試
修改成功後,無重新整理修改列表中的檔案名稱。
總結
下班回來後,折騰了一會代碼,實現了編輯檔案名稱的操作。
[實戰]MVC5+EF6+MySql企業網盤實戰(9)——編輯檔案名稱