abp(net core)+easyui+efcore實現倉儲管理系統——展現層實現增刪改查之控制器(六)


abp(net core)+easyui+efcore實現倉儲管理系統目錄

abp(net core)+easyui+efcore實現倉儲管理系統——ABP總體介紹(一)

abp(net core)+easyui+efcore實現倉儲管理系統——解決方案介紹(二)

abp(net core)+easyui+efcore實現倉儲管理系統——領域層創建實體(三)

 abp(net core)+easyui+efcore實現倉儲管理系統——定義倉儲並實現 (四)

abp(net core)+easyui+efcore實現倉儲管理系統——創建應用服務(五)

 

       通過前面三篇文章的介紹,我們學習了如何創建實體,如何創建數據庫操作,如何創建應用服務。在上一文章中我們在應用層實現了對數據庫的CURD操作。在本篇文章中,主要是使用常規的MVC方式來實現增刪改查的功能,通過完善Controller、View、ViewModel,以及調試修改控制器來實現展示層的增刪改查。最終實現效果如下圖:

 

一、創建ModuleController

      ABP對ASP.NET Net Core MVC  Controllers進行了集成,通過ABP網站創建的項目會自動創建一個Controller基類,這個Controller基類繼承自AbpController, 我們即可使用ABP附加給我們的以下強大功能:

  • 本地化
  • 異常處理
  • 對返回的JsonResult進行包裝
  • 審計日志
  • 權限認證([AbpMvcAuthorize]特性)
  • 工作單元(默認未開啟,通過添加[UnitOfWork]開啟)

      我們創建的ABP.TPLMS項目,也同樣創建了一個控制器基類,具體位置如下圖。

 

      1. 在Visual Studio 2017的“解決方案資源管理器”中,右鍵單擊在領域層“ABP.TPLMS.Web.Mvc”項目中的Controller目錄。 選擇“添加” > “新建項…”。如下圖。

 

    2. 在彈出對話框“添加新項-ABP.TPLMS.Web.Mvc”中選擇“控制器類”,然后在名稱輸入框中輸入“ModuleController”,然后點擊“添加”按鈕。如下圖。

 

      3.在Visual Studio 2017中打開我們剛才創建ModuleController.cs,並繼承自TPLMSControllerBase,並增加列表與修改方法。通過構造函數注入對應用服務的依賴。具體代碼如下。

 

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Abp.Application.Services.Dto; using Abp.AspNetCore.Mvc.Authorization; using Abp.Runtime.Validation; using ABP.TPLMS.Controllers; using ABP.TPLMS.Modules; using ABP.TPLMS.Modules.Dto; using ABP.TPLMS.Web.Models.Module; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ABP.TPLMS.Web.Controllers { [AbpMvcAuthorize] 
    public class ModuleController : TPLMSControllerBase { // GET: /<controller>/
        public IActionResult Index() { var output = _moduleAppService.GetAllAsync(); var model = new EditModuleModalViewModel { Module = AutoMapper.Mapper.Map<CreateUpdateModuleDto>(output.Result.Items.First()), Modules = output.Result.Items }; return View(model); } private readonly IModuleAppService _moduleAppService; public ModuleController(IModuleAppService moduleAppService) { _moduleAppService = moduleAppService; } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(EditModuleModalViewModel updateDto) { if (updateDto == null) { return NotFound(); } if (updateDto.Module == null) { return NotFound(); } _moduleAppService.CreateAsync(updateDto.Module); return RedirectToAction(nameof(Index)); } public IActionResult Create() { return View(); } [HttpPost] [DisableValidation] public ActionResult Edit(int id,EditModuleModalViewModel updateDto) { if (id != updateDto.Module.Id) { return NotFound(); } if (ModelState.IsValid) { try { var module= updateDto.Module; _moduleAppService.UpdateAsync(module); } catch (DbUpdateConcurrencyException) { if (!DtoExists(updateDto.Module.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(updateDto); 
 } private bool DtoExists(long id) { return _moduleAppService.GetAllAsync().Result.Items.Any(e => e.Id == id); } // GET: Cargoes/Edit/5
        public IActionResult Edit(int? id) { if (id == null) { return NotFound(); } var module =  _moduleAppService.GetAllAsync().Result.Items.SingleOrDefault(m => m.Id == id); if (module == null) { return NotFound(); } var model = new EditModuleModalViewModel { Module = AutoMapper.Mapper.Map<CreateUpdateModuleDto>(module) }; return View(model); //return Ok(cargo.Result);
 } // GET: Cargoes/Delete/5
        public  IActionResult Delete(int? id) { if (id == null) { return NotFound(); } var module = _moduleAppService.GetAllAsync().Result.Items.SingleOrDefault(m => m.Id == id); if (module == null) { return NotFound(); } var model = new EditModuleModalViewModel { Module = AutoMapper.Mapper.Map<CreateUpdateModuleDto>(module) }; return View(model); } // POST: Cargoes/Delete/5
        [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<IActionResult> DeleteConfirmed(int id) { try { await _moduleAppService.DeleteAsync(id); } catch (Exception ex) { return View(ex.Message); //throw;
 } return RedirectToAction(nameof(Index)); } } }

 

 

         


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM