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));
}
}
}
相关文章
- abp(net core)+easyui+efcore实现仓储管理系统——组织管理升级之下(六十二)
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之一(四十九)
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之十(四十六)
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之三存储过程(三十九)
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之一(三十七)
- abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之九(三十五)
- abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之八(三十四)
- abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之四(三十)
- abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理七(二十五)
- abp(net core)+easyui+efcore实现仓储管理系统——EasyUI之货物管理一 (十九)
- abp(net core)+easyui+efcore实现仓储管理系统——EasyUI前端页面框架 (十八)
- abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十二)
- abp(net core)+easyui+efcore实现仓储管理系统目录
- .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
- List多个字段标识过滤 IIS发布.net core mvc web站点 ASP.NET Core 实战:构建带有版本控制的 API 接口 ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目 Using AutoFac
- SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(23)-设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用