1. 程式人生 > >abp(net core)+easyui+efcore倉儲系統——展現層實現增刪改查之控制器(六)

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.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 = 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(CreateUpdateModuleDto updateDto)
        {
            _moduleAppService.CreateAsync(updateDto);
            var output = _moduleAppService.GetAllAsync();
            return PartialView("_List", output.Result);
        }

     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= AutoMapper.Mapper.Map<CreateUpdateModuleDto>(updateDto.Module);

                    _moduleAppService.UpdateAsync(module);           

                }
                catch (DbUpdateConcurrencyException ex)
                {
                    if (!DtoExists(updateDto.Module.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw ex;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(updateDto);          
        }

        private bool DtoExists(long id)
        {
            return _moduleAppService.GetAllAsync().Result.Items.Any(e => e.Id == id);
        }

        // GET: Module/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 = module
            };
            return View(model);
            //return Ok(cargo.Result);
        }

  // GET: Module/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: Module/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));
        }
}
}