abp(net core)+easyui+efcore实现仓储管理系统——展示层实现增删改查之控制器(六)

abp(net core)+easyui+efcore实现仓储管理系统目录

abp(net core)+easyui+efcore实现仓储管理系统——ABP整体介绍(一)html

abp(net core)+easyui+efcore实现仓储管理系统——解决方案介绍(二)数据库

abp(net core)+easyui+efcore实现仓储管理系统——领域层建立实体(三)app

 abp(net core)+easyui+efcore实现仓储管理系统——定义仓储并实现 (四)async

abp(net core)+easyui+efcore实现仓储管理系统——建立应用服务(五)函数

 

       经过前面三篇文章的介绍,咱们学习了如何建立实体,如何建立数据库操做,如何建立应用服务。在上一文章中咱们在应用层实现了对数据库的CURD操做。在本篇文章中,主要是使用常规的MVC方式来实现增删改查的功能,经过完善Controller、View、ViewModel,以及调试修改控制器来实现展现层的增删改查。最终实现效果以下图:post

 

1、建立ModuleController学习

      ABP对ASP.NET Net Core MVC  Controllers进行了集成,经过ABP网站建立的项目会自动建立一个Controller基类,这个Controller基类继承自AbpController, 咱们便可使用ABP附加给咱们的如下强大功能:网站

  • 本地化
  • 异常处理
  • 对返回的JsonResult进行包装
  • 审计日志
  • 权限认证([AbpMvcAuthorize]特性)
  • 工做单元(默认未开启,经过添加[UnitOfWork]开启)

      咱们建立的ABP.TPLMS项目,也一样建立了一个控制器基类,具体位置以下图。ui

 

      1. 在Visual Studio 2017的“解决方案资源管理器”中,右键单击在领域层“ABP.TPLMS.Web.Mvc”项目中的Controller目录。 选择“添加” > “新建项…”。以下图。spa

 

    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)); } } }
相关文章
相关标签/搜索