ABP学习入门系列(五)(展示实现增删改查)

大致要实现的 效果如下

1,添加Controller(用到的X.PagedList 注意到nuget添加)

using System.Web.Mvc;
using Abp.Application.Services.Dto;
using Abp.Runtime.Caching;
using Abp.Threading;
using Abp.Web.Mvc.Authorization;
using AutoMapper;
using LearningMpaAbp.Notifications;
using LearningMpaAbp.Tasks;
using LearningMpaAbp.Tasks.Dtos;
using LearningMpaAbp.Users;
using LearningMpaAbp.Users.Dto;
using LearningMpaAbp.Web.Models.Tasks;
using X.PagedList;

namespace LearningMpaAbp.Web.Controllers
{
    [AbpMvcAuthorize]
    public class TasksController : LearningMpaAbpControllerBase
    {
        private readonly ITaskAppService _taskAppService;
        private readonly IUserAppService _userAppService;
        private readonly INotificationAppService _notificationAppService;
        private readonly ICacheManager _cacheManager;

        public TasksController(ITaskAppService taskAppService, IUserAppService userAppService, ICacheManager cacheManager, INotificationAppService notificationAppService)
        {
            _taskAppService = taskAppService;
            _userAppService = userAppService;
            _cacheManager = cacheManager;
            _notificationAppService = notificationAppService;
        }

        public ActionResult Index(GetTasksInput input)
        {
            var output = _taskAppService.GetTasks(input);

            var model = new IndexViewModel(output.Tasks)
            {
                SelectedTaskState = input.State
            };
            return View(model);
        }

        // GET: Tasks
        public ActionResult PagedList(int? page)
        {
            //每页行数
            var pageSize = 5;
            var pageNumber = page ?? 1; //第几页

            var filter = new GetTasksInput
            {
                SkipCount = (pageNumber - 1) * pageSize, //忽略个数
                MaxResultCount = pageSize
            };
            var result = _taskAppService.GetPagedTasks(filter);

            //已经在应用服务层手动完成了分页逻辑,所以需手动构造分页结果
            var onePageOfTasks = new StaticPagedList<TaskDto>(result.Items, pageNumber, pageSize, result.TotalCount);
            //将分页结果放入ViewBag供View使用
            ViewBag.OnePageOfTasks = onePageOfTasks;

            return View();
        }

        public PartialViewResult GetList(GetTasksInput input)
        {
            var output = _taskAppService.GetTasks(input);
            return PartialView("_List", output.Tasks);
        }

        /// <summary>
        /// 获取创建任务分部视图
        /// 该方法使用ICacheManager进行缓存,在WebModule中配置缓存过期时间为10mins
        /// </summary>
        /// <returns></returns>
        public PartialViewResult RemoteCreate()
        {
            //1.1 注释该段代码,使用下面缓存的方式
            //var userList = _userAppService.GetUsers();

            //1.2 同步调用异步解决方案(最新Abp创建的模板项目已经去掉该同步方法,所以可以通过下面这种方式获取用户列表)
            //var userList = AsyncHelper.RunSync(() => _userAppService.GetUsersAsync());

            //1.3 缓存版本
            var userList = _cacheManager.GetCache("ControllerCache").Get("AllUsers", () => _userAppService.GetUsers());

            //1.4 转换为泛型版本
            //var userList = _cacheManager.GetCache("ControllerCache").AsTyped<string, ListResultDto<UserListDto>>().Get("AllUsers", () => _userAppService.GetUsers());

            //1.5 泛型缓存版本
            //var userList = _cacheManager.GetCache<string, ListResultDto<UserListDto>>("ControllerCache").Get("AllUsers", () => _userAppService.GetUsers());

            ViewBag.AssignedPersonId = new SelectList(userList.Items, "Id", "Name");
            return PartialView("_CreateTaskPartial");
        }

        /// <summary>
        /// 获取创建任务分部视图(子视图)
        /// 该方法使用[OutputCache]进行缓存,缓存过期时间为2mins
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        [OutputCache(Duration = 1200, VaryByParam = "none")]
        public PartialViewResult Create()
        {
            var userList = _userAppService.GetUsers();
            ViewBag.AssignedPersonId = new SelectList(userList.Items, "Id", "Name");
            return PartialView("_CreateTask");
        }

        // POST: Tasks/Create
        // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
        // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreateTaskInput task)
        {
            var id = _taskAppService.CreateTask(task);

            var input = new GetTasksInput();
            var output = _taskAppService.GetTasks(input);

            return PartialView("_List", output.Tasks);
        }

        // GET: Tasks/Edit/5

        public PartialViewResult Edit(int id)
        {
            var task = _taskAppService.GetTaskById(id);

            var updateTaskDto = Mapper.Map<UpdateTaskInput>(task);

            var userList = _userAppService.GetUsers();
            ViewBag.AssignedPersonId = new SelectList(userList.Items, "Id", "Name", updateTaskDto.AssignedPersonId);

            return PartialView("_EditTask", updateTaskDto);
        }

        // POST: Tasks/Edit/5
        // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
        // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UpdateTaskInput updateTaskDto)
        {
            _taskAppService.UpdateTask(updateTaskDto);

            var input = new GetTasksInput();
            var output = _taskAppService.GetTasks(input);

            return PartialView("_List", output.Tasks);
        }

        public ActionResult NotifyUser()
        {
            _notificationAppService.NotificationUsersWhoHaveOpenTask();
            return new EmptyResult();
        }
    }
}

2,视图

@using Abp.Web.Mvc.Extensions
@model LearningMpaAbp.Web.Models.Tasks.IndexViewModel

@{
    ViewBag.Title = L("TaskList");
    ViewBag.ActiveMenu = "TaskList"; //Matches with the menu name in SimpleTaskAppNavigationProvider to highlight the menu item
}
@section scripts{
    @Html.IncludeScript("~/Views/Tasks/index.js");
}
<h2>
    @L("TaskList")

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add">Create Task</button>

    <a class="btn btn-primary" data-toggle="modal" href="@Url.Action("RemoteCreate")" data-target="#modal" role="button">(Create Task)使用Remote方式调用Modal进行展现</a>

    <a class="btn btn-success" href="@Url.Action("PagedList")" role="button">分页展示</a>

    <button type="button" class="btn btn-info" onclick="notifyUser();">通知未完成任务的用户</button>
    <!--任务清单按照状态过滤的下拉框-->
    <span class="pull-right">
        @Html.DropDownListFor(
            model => model.SelectedTaskState,
            Model.GetTaskStateSelectListItems(),
            new
            {
                @class = "form-control select2",
                id = "TaskStateCombobox"
            })
    </span>
</h2>

<!--任务清单展示-->
<div class="row" id="taskList">
    @{ Html.RenderPartial("_List", Model.Tasks); }
</div>

<!--通过初始加载页面的时候提前将创建任务模态框加载进来-->
@Html.Action("Create")

<!--编辑任务模态框通过ajax动态填充到此div中-->
<div id="edit">

</div>

<!--Remote方式弹出创建任务模态框-->
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="createTask" data-backdrop="static">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

        </div>
    </div>
</div>

另外还有_createTaskPartial,_EditTaskPartial 等,这里就不贴代码了

以上。。。

参考http://www.jianshu.com/p/620c20fa511b

代码地址https://github.com/tianxiangd/LearnAbp

时间: 2024-08-01 04:22:13

ABP学习入门系列(五)(展示实现增删改查)的相关文章

JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(二)

前言:上篇 JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一) 介绍了下knockout.js的一些基础用法,由于篇幅的关系,所以只能分成两篇,望见谅!昨天就觉得应该快点完成下篇,要不然有点标题党的感觉,思及此,博主心有不安,于是加班赶出了下篇.如果你也打算用ko去做项目,且看看吧! 一.效果预览 其实也没啥效果,就是简单的增删改查,重点还是在代码上面,使用ko能够大量节省界面DOM数据绑定的操作.下面是整个整个增删改查逻辑的js代码: 页面效果: 二.

JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查

前言:之前博主分享过knockoutJS和BootstrapTable的一些基础用法,都是写基础应用,根本谈不上封装,仅仅是避免了html控件的取值和赋值,远远没有将MVVM的精妙展现出来.最近项目打算正式将ko用起来,于是乎对ko和bootstraptable做了一些封装,在此分享出来供园友们参考.封装思路参考博客园大神萧秦,如果园友们有更好的方法,欢迎讨论. KnockoutJS系列文章: JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一) JS组件

MVC3+EF4.1学习系列(二)-------基础的增删改查和持久对象的生命周期变化

上篇文章中 我们已经创建了EF4.1基于code first的例子  有了数据库 并初始化了一些数据  今天这里写基础的增删改查和持久对象的生命周期变化 学习下原文先把运行好的原图贴来上~~ 一.创建详细页 首先 我们先在控制器下 添加详细页的方法 因为这篇文章后面要介绍持久对象声明周期的变化 所以在这里先看下有哪些状态 EF里一共有这五中生命状态类型 其实 看名字我们可以大概猜测出个一二三来~~  游离的 未改变的  新添加的  已删除的 修改的  但是是怎么变化的能 我们在后面的代码中实践与

Sql Server2008温故而知新系列02:数据增删改查之&quot;增&quot;

增删改查-数据库最基本使用方法,也是数据库最常用的操作方法: 用到的命令:insert[into] 插入:delete from  删除:update 修改:select 查询. 首先说一说插入的格式(即新增数据): 1.insert into table_name(field1,field2,field3,…………)  values(字段1记录,字段2记录,…………) 如有多行记录重复写入多行 2.insert into table_name(field1,field2,field3,…………

权限组件(8):一级菜单的展示、增删改查和保留原参数

效果图: 一.路由配置 rbac/urls.py from django.urls import re_path from rbac.views import menu urlpatterns = [ ... # 菜单管理 re_path(r'^menu/list/$', menu.menu_list, name='menu_list'), re_path(r'^menu/add/$', menu.menu_add, name='menu_add'), re_path(r'^menu/edit/

ABP学习入门系列(一)

ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称.ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用程序的新起点,它旨在成为一个通用的WEB应用程序框架和项目模板.框架ABP是基于最新的ASP.NET CORE,ASP.NET MVC和Web API技术的应用程序框架.并使用流行的框架和库,它提供了便于使用的授权,依赖注入,验证,异常处理,本地化,日志记录,缓存等常用功能.架构ABP实现了多层架构(领域

ABP学习入门系列(二)

本文将介绍在ABP框架中使用代码创建一个数据库表 1.下图是abp的体系结构. 我们要是实现创建数据库表的功能主要就是在下图中domain(领域层)做相应的一些操作. 2,看一下解决方案,如下图. core就是我们要做相应修改的项目  从上至下依次 application (应用服务层),core(领域层),EntityFramework(基础设施层),web和webapi (Web和展现) 3,在core下创建Tasks文件夹,在文件夹下创建Task类. using System; using

3、ASP.NET MVC入门到精通——Entity Framework增删改查

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 这里我接上讲Entity Framework入门.从网上下载Northwind数据库,新建一个控制台程序,然后重新添加一个ado.net实体数据模型. EF中操作数据库的"网关"(操作上下文) DBContext封装 .NET Fra

MySQL数据库学习【第三篇】增删改查操作

注意:1.如果你在cmd中书命令的时候,输入错了就用\c跳出 2.\s查看配置信息 一.操作文件夹(库) 增:create database db1 charset utf8; 删:drop database db1; 改:alter database db1 charset gbk; 查:show databases; #查看所有的数据库 show create database db1; #查看db1数据库 二.操作文件(表) 切换到文件夹下:use db1 增:create table t